Friday, July 5, 2024
HomeLanguagesJavascriptHow to declare variables in different ways in JavaScript?

How to declare variables in different ways in JavaScript?

In JavaScript, we can declare a variable in different ways by using different keywords. Each keyword holds some specific reason or feature in JavaScript. Basically, we can declare variables in three different ways by using var, let and const keywords. Each keyword is used in some specific conditions.

JavaScript var

JavaScript let

JavaScript const

Can be redeclared Cannot be redeclared Cannot be redeclared
Can be reassigned a value Can be reassigned a value Cannot reassign the value
Only have global and function scope Can have a block scope Can have a block scope
Variables are hoisted on top and can be used anywhere Variables must be initialized before use Variables must be initialized before use
Can be redeclared anywhere in the program Can be redeclared inside a block Can never be redeclared

JavaScript var: This keyword is used to declare variables globally. If you used this keyword to declare a variable then the variable can accessible globally and changeable also. It is good for a short length of codes, if the codes get huge then you will get confused.

Syntax:

var variableName = "Variable-Value;"

Example: 

javascript




<script>
    var neveropen = "neveropen";
    console.log(neveropen);
</script>


Output:

neveropen

JavaScript let: This keyword is used to declare variable locally. If you used this keyword to declare a variable then the variable can accessible locally and it is changeable as well. It is good if the code gets huge.

Syntax:

let variableName = "Variable-Value;"

Example: 

javascript




<script>
if (true) {
    let neveropen = "neveropen";
    console.log(neveropen);
    }
     
    /* This will be error and
       show neveropen is not defined */
    console.log(neveropen);
</script>


Output:

neveropen

JavaScript const: This keyword is used to declare variable locally. If you use this keyword to declare a variable then the variable will only be accessible within that block similar to the variable defined by using let and difference between let and const is that the variables declared using const values can’t be reassigned. So we should assign the value while declaring the variable.

Syntax:

const variableName = "Variable-Value;"

Example: 

javascript




<script>
    const neveropen = "neveropen";
    console.log(neveropen);
</script>


Output:

neveropen

 

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments