JS Tutorial

Function Reference

Method Reference

  • JS String
  • JS Array
  • JS var,let and const

    var, let and const will be discussed with their scope, use and hoisting.

    var keyword

    • Variable declaration are processed before the execution of the code, means variable hoisting
    • The scope of JavaScript variable declared outside the function is global

    let keyword

    • Let allows us to create a variable with the scope limited to the block on which it is used.

    const keyword

    • const is same as var, but can not re-assign the value nor re-declared
    • In case of object we can re-assign the value because the object is immutable

    var keyword

    Scope:var variable are globally scoped or function scoped

    Example 1- global scope

    var test=10;
    function testJS()
    {
    console.log(test);
    }
    testJS();//10
    

    Example 2 - function scope

    function testJS()
    {
    var test=10;
    
    }
    testJS();
    console.log(test);//error: test is not defined
    

    var variables can be re-declared and updated too.

    var test =10;
    var test =15;
    OR
    var test =10;
    test =15;
    

    Hoisting of var

    In js a variable(var) can be used before it has been declared.

    Example 3

    test=5;
    console.log(test);//5
    var test;
    

    Example 4

    var test;
    console.log(test); //undefined
    test=10;
    console.log(test); //10
    
    Variables and constants declared with let or const are not hoisted

    problem with var Variables , it's override block scope by function scope

    var test=10;
    if(test>5)
    {
    var test=5;
    }
    console.log(test);//5
    

    let keyword

    Scope:let is block scoped {}.

    let test=10;
    if(test>5)
    {
    let test=5;
    }
    console.log(test);//10
    
    let test=10;
    if(test>5)
    {
    test=5;
    }
    console.log(test);//5
    

    let variables can be updated but not re-declared

    let test=10;
    test=5;
    
    let test=10;
    let test=5;//error: Identifier 'test' has already been declared
    

    const keyword

    const declarations are block scoped{}, it' same as let but const variable cannot be updated or re-declared.

    Example 5 - Scope

    const test=10;
    if(test>5)
    {
    const test=5;
    console.log(test);//5
    }
    console.log(test);//10
    

    Redeclare

    Example 6

    const test=10;
    test=5;//Uncaught TypeError: Assignment to constant variable.
    

    Example 7

    const test=10;
    const test=5;//Uncaught SyntaxError: Identifier 'test' has already been declared
    

    Example 8 - (Re-assign in case of Object)

    const test={'name':'JS'};
    test.name='JS6';
    console.log(test.name);//JS6
    

    Const Hoisting

    test=5;
    console.log(test);
    let test;
    //Uncaught ReferenceError: Cannot access 'test' before initialization
    

    var vs let vs const

    • var declarations are globally or function scoped,let and const are block scoped{}.
    • var variables can be updated and re-declared within its scope, let variables can be updated but not re-declared,const variables neither updated nor re-declared.
    • var and let can be declared without initializing it, const must be initialized during declaration.
    • Only var variables are hoisted on the top,let and const variables are not initialized