</> Code Corner

JavaScript · JS Tutorial · Lesson 8

JS Let & Const

Block scope, redeclaring and reassigning.

Variables declared with let and const have block scope: they only exist inside the { } block where they are declared.

Example

<p id="demo"></p>
<script>
let x = 10;
{
  let x = 2;     // a different x, only inside this block
}
const colors = ["red", "green"];
colors.push("blue");   // allowed: the array is changed, not reassigned
document.getElementById("demo").innerHTML = "x = " + x + ", colors = " + colors;
</script>
Scope Redeclare Reassign
var Function Yes Yes
let Block No Yes
const Block No No
Tip: const does not make values immutable — it makes the binding constant. Arrays and objects declared with const can still be changed.

Keep reading “JS Let & Const”

Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.

Chat with us 👋