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
}

< Read from Text File   |   Global Variables >

© 2019 SyntaxDB. All Rights Reserved.