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 keyword. Each keyword is used in some specific conditions.
var: This keyword is used to declare variable globally. If you used this keyword to declare 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 = "Varable-Value;"
- Code:
<script>vargeeks ="GeeksforGeeks";console.log(geeks);</script>chevron_rightfilter_none - Output:
GeeksforGeeks
let: This keyword is used to declare variable locally. If you used this keyword to declare variable then the variable can accessible locally and it is changeable as well. It is good if the code gets huge.
- Syntax:
let variableName = "Varable-Value;"
- Code:
<script>if(true) {let geeks ="GeeksforGeeks";console.log(geeks);}/* This will be error andshow geeks is not defined */console.log(geeks);</script>chevron_rightfilter_none - Output:
GeeksforGeeks
const: This keyword is used to declare variable globally. If you used this keyword to declare variable then the variable can accessible globally and it is not changeable.
- Syntax:
var variableName = "Varable-Value;"
- Code:
<script>const geeks ="GeeksforGeeks";console.log(geeks);</script>chevron_rightfilter_none - Output:
GeeksforGeeks
Recommended Posts:
- How to declare the optional function parameters in JavaScript ?
- How to declare a global variable in PHP?
- How to declare nullable type in TypeScript ?
- JavaScript Course | Variables in JavaScript
- Creating objects in JavaScript (4 Different Ways)
- Variables and Datatypes in JavaScript
- How to unset JavaScript variables?
- How to pass JavaScript variables to PHP ?
- How to pass variables and data from PHP to JavaScript ?
- Global and Local variables in JavaScript
- How to unpack array elements into separate variables using JavaScript ?
- What are undeclared and undefined variables in JavaScript?
- View the list of all variables in Google Chrome Console using JavaScript
- How to use JavaScript variables in jQuery selectors ?
- Reference and Copy Variables in JavaScript
- Different ways for passing data to view in Laravel
- PHP | $_FILES Array (HTTP File Upload variables)
- Variables in TypeScript
- How to pass PHP Variables by reference ?
- CSS | Variables
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


