Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

Example using const

const x = 5;
const y = 6;
const z = x + y;

Mixed Example

const price1 = 5;
const price2 = 6;
let total = price1 + price2;

The const keyword is utilized to declare the variables price1 and price2, signifying that they hold constant values which cannot be altered.

On the other hand, the variable total is declared using the let keyword, indicating that its value can be modified.

When to Use var, let, or const?

  1. It’s essential to declare variables explicitly.
  2. Utilize const whenever the value is meant to remain unchanged.
  3. Employ const when the type of variable, such as Arrays and Objects, should not be altered.
  4. Resort to let only if const isn’t feasible for the scenario.
  5. Limit the use of var to cases where support for older browsers is mandatory.

Just Like Algebra

Similar to algebra, variables in programming store values.

let x = 5;
let y = 6;

As in algebra, variables are employed within expressions in programming.

let z = x + y;

Based on the provided example, it can be inferred that the total is computed as 11.

Note

Variables serve as containers for storing values.