GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • About
  • Reference
  • Integrations
  • API
  • Blog
  • JavaScript Syntax Reference
  • Feedback

This view

  • Original page
  • New address

GeezerVison is showing syntaxdb.com — Variable Declaration in JavaScript - SyntaxDB - JavaScript Syntax Reference

  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Languages
    • User Program Communication
      • Console Output
      • Commenting
      • Read from Text File
    • Variables
      • Variable Declaration
      • Global Variables
      • Arrays
      • Determine Type
    • Control Flow
      • If Statement
      • Switch Case
      • Ternary Operator
      • For Loop
      • For Each
      • While Loop
      • Do-While Loop
      • Try Catch Block
    • Functions
      • Function Declaration
      • Anonymous Functions
      • Closures
      • Function Assignment
    • Objects
      • Object Declaration
      • Methods
      • Create Object using Constructor
      • Constructor
Categories
Language
JavaScript
  • Java
  • C
  • C++
  • C#
  • Python
  • Ruby
  • JavaScript
  • Swift
  • Go
Version: 1.8.5

Categories

  • User Program Communication
    • Console Output
    • Commenting
    • Read from Text File
  • Variables
    • Variable Declaration
    • Global Variables
    • Arrays
    • Determine Type
  • Control Flow
    • If Statement
    • Switch Case
    • Ternary Operator
    • For Loop
    • For Each
    • While Loop
    • Do-While Loop
    • Try Catch Block
  • Functions
    • Function Declaration
    • Anonymous Functions
    • Closures
    • Function Assignment
  • Objects
    • Object Declaration
    • Methods
    • Create Object using Constructor
    • Constructor

Variables

Variable Declaration in JavaScript

Used to declare a variable.


Syntax
let variableName; //block scope
var variableName; //function scope

let variableName = value; //variables can be initialized with a value

Notes

A var has function scope, which means it can be accessed throughout the entire function. A let has block scope, which means it can only be accessed within the block of code.

Variables in JavaScript are dynamically typed.


Example
var x = 0;
while(x < 10) {
    let y = 1;
    x = x + y; //y can only be accessed within the while block
}

See Also
Documentation

var - JavaScript | MDN

let - JavaScript | MDN

Add to Slack

< Read from Text File   |   Global Variables >


You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.