Articles β JAVASCRIPT β Let In Javascript
Let In Javascript
Purpose
βLetβ Cannot Be Redeclared
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script>
alert("Hi");
let x = 12;
let x = "Karan";
alert(x);
var y = 12;
var y = "Karan";
alert(y);
</script>
</head>
<body>
</body>
</html>
Block Scope
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script>
{
let x = 2
var y = 4;
}
// This code will give error
alert(x);
// This code will run successfully
alert(y);
</script>
</head>
<body>
</body>
</html>