Skip to content

5.5 Const

Const Declaration

Task 1: Declare a variable called num, and set its value to 10.

var num = 10;

console.log(num);

The result is: 10

Task 2: Re-do task 1, and re-declare num with const keyword.

const num = 10;

console.log(num);

The result is: 10

Task 3: Refer to task 2; assign the value of 20 to num variable.

const num = 10;

num = 20;
console.log(num);

The result is: Uncaught TypeError: Assignment to constant variable.

1- const

The value of a constant can not be changed.


Declaration & Assigning

Task 4: Declare a variable called name with var keyword, then assign "Sami" to it.

var name;

name = "Sami";
console.log(name);

The result is: Sami

Task 5: Re-do task 4, however declare the variable with const keyword.

const name;

name = "Sami";
console.log(name);

The result is: Uncaught SyntaxError: Missing initializer in const declaration

2- const

The const variables must be assigned when they are declared.


Constant Values

Task 6: Declare a variable x with const keyword, and assign 145 to it.

const x = 145;

console.log(x);

The result is: 145

Task 7: Declare a variable myObj with const keyword, and assign {a: 1, b: 2} to it.

const myObj = {a: 1, b: 2};

console.log(myObj);

The result is: {a:1, b:2}

Task 8: Refer to task 7; update myObj.b to 20.

const myObj = {a: 1, b: 2};

myObj.b = 20;

console.log(myObj);

The result is: {a: 1, b: 20}

3- const

The const variable value is not a real constant value. However, it defines a constant reference to a value. Therefore, you can update the constant objects.


Const Scope

Function Scope

Task 9: Log the value of local variable outside the following function.

function func(){
  var local = "Hey, I am local variable";
  return local;
}

console.log(local);

The result is: Uncaught ReferenceError: local is not defined

Task 10: Log the value of local variable outside the following function.

function func(){
  const local = "Hey, I am local variable";
  return local;
}

console.log(local);

The result is: Uncaught ReferenceError: local is not defined

Function Scope

Function scope is within the function.

Block Scope

Task 11: Log the value of myBlockVar variable outside curly brackets {}.

{
  var myBlockVar = "I am inside a block";
}

console.log(myBlockVar);

The result is: I am inside a block

Task 12: Log the value of myBlockVar variable outside curly brackets {}.

{
  const myBlockVar = "I am inside a block";
}

console.log(myBlockVar);

The result is: Uncaught ReferenceError: myBlockVar is not defined

Task 13: Log the value of myBlockVar variable outside the if statement.

if(true){
  var myBlockVar = true;
}

console.log(myBlockVar);

The result is: true

Task 14: Log the value of myBlockVar variable outside the if statement.

if(true){
  const myBlockVar = true;
}

console.log(myBlockVar);

The result is: Uncaught ReferenceError: myBlockVar is not defined

Block Scope

Block Scope is withing curly brackets

4- const

The const variables are block-scoped.


Extra Practice

Task 15: Create PI constant with the value 3.14.

const PI = 3.14;

console.log(PI);

The result is: 3.14

Task 16: Refer to task 15; can you assign 3 to PI constant?

const PI = 3.14;

PI = 3;

console.log(PI);

The result is: Uncaught TypeError: Assignment to constant variable.

Task 17: Create a regular function to check if the result of multiplying e constant by a given number is greater than 1 or not. The value of e is 2.718.

function greaterThanE(num){
  const e = 2.718;
  return (e * num ) > 1;
}

console.log(greaterThanE(5));
console.log(greaterThanE(0.0006));

The result is as follows:

true
false

Task 18 Refer to Task 17; can you access e outside the greaterThanE function?

function greaterThanE(num){
  const e = 2.718;
  return (e * num ) > 1;
}

console.log(e);

The result is: Uncaught ReferenceError: e is not defined

Task 19: Declare the secret constant in a block, and try to access its value outside the block. The value of secret constant is "It is a secret, so how I am supposed to share with you ;)".

{
  const secret = "It is a secret, so how I am supposed to share with you ;)";
}

console.log(secret);

The result is: Uncaught ReferenceError: secret is not defined

Task 20: Re-do task 19, however replace const with var.

{
  var secret = "It is a secret, so how I am supposed to share with you ;)";
}

console.log(secret);

The result is: It is a secret, so how I am supposed to share with you ;)

Task 21: Create a regular function that takes income, and checks if it is greater than 1000, it applies 0.16 tax on it; otherwise, it applies 0.06 tax on it. Hint: tax values are constant.

function calcTax(income){
  if(income > 1000){
    const tax = 0.16;
  }else{
    const tax = 0.06;
  }
  return tax * income;
}

console.log(calcTax(500));

The result is: Uncaught ReferenceError: tax is not defined

Task 22: Re-do task 21 to achieve the correct result.

function calcTax(income){
  if(income > 1000){
    const tax = 0.16;
    return tax * income;
  }else{
    const tax = 0.06;
    return tax * income;
  }
}

console.log(calcTax(500));

The result is: 30