JavaScript var, let and const

 JavaScript var, let and const

In this article we are goanna to see about Reserved Keywords of JavaScript which is most important when you are going to start your career in JavaScript. Please read and enjoy this article if you want to give any feedback about your experience, we welcome.




var = The scope of a variable declared with var is its current execution context, which is either the enclosing function or, for variables declared outside any function, global.

let = let allows you to declare variable that are limited in scope to the block, statement, or expression on which it is used.

const = This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the window object, unlike var variables. An initializer for a constant is required, that is you must specify its value in the same statement in which it's declared which can't be changed later. 

Example of var, let and const define:




<html>
<head>
<title>JavaScript Tutorial</title>
</head>
<body>
<h1>Var, let and const JavaScript</h1>
<script type=“text/javascript”>
var a = 10;
let b = 20;
const c = 20;
document.write(a);
document.write(b); 
document.write(c);
</script>
</body>
</html>

var and let in function example:

<html>
<head>
<title>JavaScript Tutorial</title>
</head>
<body>
<h1>var JS</h1>
<script type=“text/javascript”>
var a = 10;
{
var a = 20;
document.write(a + '<br>');
}
document.write(a + '<br>');
</script>
</body>
</html>


<html>
<head>
<title>JavaScript Tutorial</title>
</head>
<body>
<h1>let JS</h1>
<script type=“text/javascript”>
let a = 10;
{
let a = 20;
document.write(a + ‘<br>’);
}
document.write(a + '<br>');
</script>
</body>
</html>





Share:

0 comments

Please leave your comments...... Thanks