Author -  Sai gowtham

When to use const keyword over var in JavaScript

In this tutorial, we are going to learn about when to use a const keyword instead of var keyword in JavaScript.

const: In JavaScript, if we declare a variable by using const keyword we can’t reassign a new value to that variable identifier.

  • const keyword is block scoped and we also see an error if we try to access any variable before initialized.

var: If we declare a variable by using var keyword we can reassign a new value to that variable identifier.

What is the Identifier?

The name we are used to identifying the variables is called identifier.

Let’s see some examples.

const keyword usage example


const name = "reactgo.com";

// we cant't assign the new value to const variable
name = "king" //TypeError: Assignment to constant variable.


//block scoped
if(true){
     const hello = "hello world"
     console.log(hello) // "hello world"
}

 //we can't access the variable hello outside the block scope.

console.log(hello) // Uncaught reference error

We can’t reassign any new value to const keyword variable identifier but we can mutate the objects by using keys and arrays values, like i shown in below example.


const user = {
    name: "example1",
    age: 5
}

// we can't assign a new object
 user =  { } // not possible

// it is possible
user.name = "example2";

// arrays
const colors = ['red','green','blue','yellow'];

// we can't assign a new array
colors = [ ] // not possible

// it's possible
colors[0] = "olive"

colors.push('orange');

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY