JavaScript - Comments & Variables

Tokens :

It is a smallest unit of any programming language which consists
  • Keywords
  • Identifiers
  • Literals / Values

    i. Keywords
    It is a predefined reserved words which can be understandable by the javscript engine. It should be always in lowercase.

    ii. Identifiers
    Name given by the programmer to the components of javascript like variables, functions etc.,

    Rules :
  • An Identifier should not start with number.
  • Except $ and _(underscore) no special characters are allowed.
  • We can't use ke yword as identifiers.

    iii. Variables / Literals :
    A named block of memory which is used to store a value is known as variables. It is a container to store data.

    Note :
  • In javascript variables are not strictly typed, it is a dynamically typed, therefore it is not necessary to specify the type of data during variable declaration.
  • In a variable we can store any type of values.
  • It is mandatory to declare a variable before using it.

    Variables :

    In JavaScript, variables are used to store and manage data. They are created using the var, let, or const keyword. 1. var Keyword : The var keyword is used to declare a variable. It has a function-scoped or globally-scoped behavior var x = 10;
    
      Syntax:
    
       va/let/const                 identifier
       (keywords)        (variable declaration & initialization )
    
         var                  a=10;
    
         Eg:
          var a = "Welcome to  Tesdbacademy"
          var b = 10;
          var c = 12;
          var d = b + c;
    
          console.log(a);
          console.log(b);
          console.log(c);
          console.log(d);
    
    
    Output:
    Welcome to Tesdbacademy
    10
    12
    22

    2. let Keyword :
    The let keyword is a block-scoped variables. It’s commonly used for variables that may change their value. let y = "Hello";
      Eg:
      let a = " Welcome to Tesdbacademy"
      let b = "joining";
      let c = " 12";
      let d = b + c;
    
      console.log(a);
      console.log(b);
      console.log(c);
      console.log(d);
    
    
    Output:
    Welcome to Tesdbacademy
    joining
    12
    joining 12

    3. const Keyword :
    The const keyword declares variables that cannot be reassigned. It’s block-scoped as well. const PI = 3.14;
      Eg:
      const a = " Welcome to Tesdbacademy"
      console.log(a);
    
      const b = 400;
      console.log(b);
    
      const c = "12";
      console.log(c);
      // Can’t change a value for a constant
      // c = "new"
      // console.log(c) will show error
    
    Output:
    Welcome to Tesdbacademy
    400
    12

    Difference of var, let and const keyword?

                Scope   Redeclare    Reassign   Hoisted     Binds this
                -----   ---------   ---------   --------    -----------
          var     No       Yes        Yes         Yes         Yes
          let     Yes       No        Yes          No          No
          const   Yes       No         No          No          No
    


    (JS - Datatypes & Hoisting)