Wednesday, 21 September 2022

ES6 Variables

 Block scope -  Block scoping means declaring a variable, not just inside a function, but around any curly brackets like if statements or loops.

Var      outside of a function, it belongs to the global scope.

     inside of a function, it belongs to that function.

inside of a block like loop etc, still avail outside the block


  let     it block scoped


const       once it is created, it cant be changed.  it is block scoped
               it defines constant reference value



Because of this you can NOT:

👉Reassign a constant value
👉Reassign a constant array
👉Reassign a constant object


But you CAN:

👉Change the elements of constant array
👉Change the properties of constant object

Arrow function

 hello = function() {

  return "Hello World!";

}



hello = () => {

  return "Hello World!";

}



hello = () => "Hello World!";


"If the function has only one statement, and the statement returns a value, you can remove the brackets and the return keyword"

"This works only if the function has only one statement."


hello = (val) => "Hello " + val;

If you have parameters, you pass them inside the parentheses


hello = val => "Hello " + val;

if you have only one parameter, you can skip the parentheses as well


js basic code understanding

Basic concept



STEP1 -  we created a class by {}, then created vars and methods () inside the class. properties are assigned inside a constructor() method.
                    class Car {
  constructor(name) {
    this.brand = name;
  }
  
  present() {
    return 'I have a ' + this.brand;
  }
}

STEP2 - create instance of class. 
        const mycar = new Car("Ford");

STEP3 - we call class method by class instance.
        mycar.present();


class Car {
  constructor(name) {
    this.brand = name;
  }
  
  present() {
    return 'I have a ' + this.brand;
  }
}

const mycar = new Car("Ford");
mycar.present();


ES6 Variables

  Block scope -  Block scoping means declaring a variable, not just inside a function, but around any curly brackets like if statements or ...