Skip to content

Latest commit

 

History

History
executable file
·
198 lines (153 loc) · 2.56 KB

File metadata and controls

executable file
·
198 lines (153 loc) · 2.56 KB

Const

  • Const declared immutable variables.
  • The variable is mutable.
  • But for an object, we can still change properties of a const object.
  • To make an object immutable we use the Object.freeze(...) function.

Q1

const foo;

Q1-Answer

// You can see the syntax error if it is missing inisialize. 
// SyntaxError: Missing initializer in const declaration

Q2

const foo=1;
foo=2;

Q2-Answer

//Const variable isn't supposed to change over time
// so we're not allowed to give a different.
//TypeError: Assignment to constant variable.

Q3

function test(){
  if(true){
    const tmp = 123;
  }
  console.log(tmp);
}

test(); 

Q3-Answer

//tmp is not defined

Q4

let foo = "foo";
foo="moo";
console.log(foo); 

Q4-Answer

  • let is mutable
  • The mutable means is that it can change over time.
//moo

Q5

const foo = {};
foo['prop'] = 'moo';
console.log(foo); 

Q5-Answer

  • const is mutable that value is object and add the properties.
//{prop: "moo"}

Q6

  • Non-strict mode
const foo = Object.freeze({});
foo['prop'] = 'Moo';
console.log(foo.prop); 

Q6-Answer

//undefined

Q7

  • strict mode
'use strict';

const foo = Object.freeze({});
foo['prop'] = 'Moo';
console.log(foo.prop); 

Q7-Answer

//TypeError: Cannot add property prop, object is not extensible

From MDN - const

  • Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.

Q8

const number = 42;
try {
  number = 99;
} catch(err) {
  console.log(err);
}
console.log(number);

Q8-Answer

> TypeError: Assignment to constant variable.
> 42

Q9

for (const property in {a: 1, b: 2}) {
  setTimeout(() => {
    console.log(property);
  }, 100);
}

Q9-Answer

  • for (const property in object) works because with each iteration you get a new variable, which is scoped only to that iteration. You can easily check that by using a closure inside a loop:

    This logs a and b, but if you change const to var, it logs b twice.

a
b

Q10

for (var property in {a: 1, b: 2}) {
  setTimeout(() => {
    console.log(property);
  }, 100);
}

Q10-Answer

b
b

Q11

for (let property in {a: 1, b: 2}) {
  setTimeout(() => {
    console.log(property);
  }, 100);
}

Q11-Answer

b
b