HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to JavaScriptVariables (let / const)Data TypesOperatorsStringsArraysObjectsConditions (if / else)Loops (for / while)FunctionsArrow Functions (=>)Array Methods (map / filter / reduce)
REFERENCE
📖 Overview
🖥️ Output & Console
📦 Variables
let / constvar (legacy)
🔢 Numbers & Math
✍️ String
📋 Array
🗂️ Object
🔁 Condition & Loop
🧩 Function
🔄 Type & Conversion
JavaScript›REFERENCE›Variables
📦

Variables

Storing values: let, const, and var.

CommandDescription
let / constDefines variables; const does not change.
var (legacy)Old variable declaration (now let/const is preferred).

let / const

Try it yourself →

Defines variables; const does not change.

Syntax

let name = value;
const constant = value;

Description

You define variables that can be changed later with let, and unchanging (constant) variables with const. Default choice is const; use let only if it will change.

Returns

—

Example

let counter = 0;
counter = 1;
const pi = 3.14;
console.log(counter, pi);

Expected output

1 3.14

When to use?

Storing all kinds of data. const makes code safer (prevents accidental changes).

Common mistakes

  • ⚠️ Reassigning to a const throws an error (TypeError).
  • ⚠️ Using a variable before defining it throws an error.

var (legacy)

Try it yourself →

Old variable declaration (now let/const is preferred).

Syntax

var name = value;

Description

var is JavaScript's old variable declaration. Since its scope rules are confusing, it has been replaced by let and const in modern code.

Example

var x = 5;
console.log(x);

Expected output

5

When to use?

You will see it in old code; write let/const in new code.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code