20 Feb JavaScript Variables
Variable names in JavaScript are the names given to locations in memory. Consider, the JavaScript variable as a container to hold values. The JavaScript variables are declared with any of the following keywords:
- var: Added to JavaScript before 2015.
- let: Added to JavaScript in 2015
- const: Added to JavaScript in 2015
As you can see above, the let and const keywords were introduced in ES2015 I.E. ES6. For JavaScript usage in the year 2023, we suggest using let. It works for all the major modern web browsers.
Integer Variable in JavaScript
Let us now declare integer variables using the let keyword. Here, we have declared an integer variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <body> <h2>The let keyword</h2> <p id="test"></p> <script> let a = 10; document.getElementById("test").innerHTML = "Value = " + a; </script> </body> </html> |
Output
String Variable in JavaScript
Let us now declare string variables using the let keyword. Here, we have declared a string variable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<!DOCTYPE html> <html> <body> <h2>The let keyword</h2> <p id="test"></p> <script> let name = "Amit"; document.getElementById("test").innerHTML = "Student Name = " + name; </script> </body> </html> |
Output
No Comments