codegurukool.com · Free coding tutorials & online compiler

Home › JavaScript Tutorial › Variables and types

Variables and types

Store values with let and const.

Use const by default and let when the value changes. Common types are string, number, boolean, array and object.

Commands in this lesson

const

Definition: Declares a variable that cannot be reassigned.

Syntax:

const name = value;

Example:

const pi = 3.14;
console.log(pi);

let

Definition: Declares a variable that can change. It lives inside its block.

Syntax:

let name = value;

Example:

let n = 1;
n = n + 1;
console.log(n);

Example code

<p id="o"></p>
<script>
  const name = "Ada";
  let age = 36;
  age = age + 1;
  document.getElementById("o").textContent = name + " is " + age;
</script>

Run this code in the online editor

← All commandsJavaScript installation →