keyWords in javaScript

https://medium.com/@sharmapriyanka84510/keywords-in-javascript-8a280ad3bc8d

Variables

  • Variables are used to store data
  • By using variables we can store all type of data like string boolean, number, arrays, and objects
  • We declare a variable by using the following keywords: var, let, const
  • “let” and “const” introduced in ES5

String

  • We can define a string in three ways: “ “, ‘ ‘ and (backtick)
  • Backtick operator introduced in ES6 and used to define multiline strings.
var sub = "Javascript";
var wish = `Welcome to ${sub} program.`
console.log(wish);
//Welcome to Javascript program.

var userName = "lassie";
var userPassword= "lassie";
var tableName = "loginDetails";
var query = `select * from ${tableName} where
             userName = "${userName}" and
             userPassword  = "${userPassword }"`
console.log(query);
/* select * from loginDetails where userName = "lassie" and userPassword = "lassie" */

Numbers

(decimal, double, hexadecimal, octal and binary)

  • Decimal numbers should prefix with “0x”
  • Octal numbers should prefix with “0o”
  • Binary numbers should prefix with “0b”
var decimal = 100;
var double = 100.123456;
var hexadecimal = 0x87486ABC;
var octal = 0o123465;
var binary = 0b1010;

console.log(decimal, double, hexadecimal, octal, binary);
/* 100 100.123456 2269670076 42805 10 */

Boolean

  • Possible values are true or false
var flag = true;
console.log(flag);
/* true */

Array

  • Collection of an indexed element called as an array
  • We declare an array by using
  • We access array data by using “indexes” which starts with 0
  • We can iterate array by using for(), forEach(), for…of()
var array1 = [10, 20, 30, 40, 50];
var array2 = [60, 70, 80, 90, 100];

for (var i=0; i < array1.length; i++) {
  console.log(array1[i]);                       
}
array1.forEach((element, index) => {
  console.log(element, array2[index]);
});
/*
10
20
30
40
50
10 60
20 70
30 80
40 90
50 100
*/
for(var value of array1) {
  console.log(value);
}
/*
10
20
30
40
50
*/

“let” keyword

  • It’s introduced in ES5 and used to declare the variables
  • “var” keyword breaks the scope rule
  • “let” keyword obeys the scope rule
for(let i=0; i<10; i++) {
  
}
console.log(i);
/* ReferenceError: i is not defined */

  • “var” keyword allows the duplicate variables
  • “let” keyword won’t allow duplicate variables
let data = 100;
let data = 100;
console.log(data);
/* SyntaxError: Identifier 'data' has already been declared */
  • If we try to access the variable before its declaration and initialization variable keyword allows and gives as result as “undefined”
  • Undefined assigned by JavaScript, issue technically called as variable hoisting
  • We can overcome variable hoisting by using “let” keyword
console.log(data);
let data = 100;
/* ReferenceError: data is not defined */
  • If any block code affecting global member then such type of issue called a global polluting issue
  • The global polluting issue raised because of the “var” keyword
  • We can overcome the global polluting issue by using either “let” or IIFE (Immediately-invoked Function Expression)
  • IIFE introduced in ES9
    /* Example 1 */
    var data = 100; {
      var data = 200;                                                   
    }
    console.log(data);
    //var:200

    /* Example 2 */
    var data1 = 100; {
      ((data1) => {
        var data1 = 200;
      })
      (data1);
    }
    console.log(data1);
    //100

    /* Example 3 */
    var data = 10;
    if(data < 100) {
      let data = 101;
      console.log(data); //101
    } else {
      let data = 201;
      console.log(data);
    }
    console.log(data); //10

    /* Example 4 */
    for(var i=0; i < 5; i++) {
      ((i) => {
        setTimeout(() => {
          console.log(i);
        }, 5000)
      }) (i);
    };
    /*
    0
    1
    2
    3
    4
    */

“const” keyword

  • “const” keyword is used to declare the variables
  • Reassignment isn’t possible by using “const” keyword but we can change internal data
/* Example 1 */
const land = "3000 sqft";
console.log(land); //3000 sqft
land = 4000 sqft;
console.log(land); //4000 sqft

/* Example 2 */
const obj = {
  floors: 1
};
obj = {};
/* TypeError: Assignment to constant variable. */

/* Example 3 */
const db {
  password: "helloworld";
};
db.password = "helloworld12345";
console.log(db);
/* {password: "helloworld12345"} */
db = {};
/* TypeError: Assignment to constant variable. */
5 Likes