Jawaria Alvi
By Jawaria Alvi

A little about const and let keywords in Javascript

A little about const and let keywords in Javascript

cons and let keywords

  1. const and let both are the keywords for variable declaration.

  2. const and let both are the block scope.

### Block Scope Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
      const blockVariableExample = () => {

            let number = 123;

            console.log(number) // Prints 123

       }

       

       blockVariableExample;

       console.log(number) // Here it gives error as number varaible can only be accessed inside blockVariableExample function

  1. const cannot be redeclared or updated while we can redeclare let variables.

    const Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
      const number = 123
    
          
    
      // Updating a const variable
    
      number = 324 // Gives error assignment to const variable
    
          
    
      // Redeclaring a const variable
    
      const number = 567 // Gives error identifier number has already been declared
    
    

    let Example

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
      let number = 123
    
          
    
      // Updating a let variable
    
      number = 324 // It updates the variable number to 324
    
          
    
      // Redeclaring a let variable
    
      let number = 567 // Gives error identifier number has already been declared