Skip to content
GitHub

Working with Variables in JavaScript

Switch to Zen Mode

  • in JavaScript, variables are containers for storing data values .
  • They allow you to name and reuse values (e.g., numbers, strings, booleans, objects) throughout your code.


JavaScript offers three keywords for declaring variables: var, let, and const. Each has distinct behavior.

  • The traditional way to declare variables. It has function scope or global scope if declared outside a function.
  • Variables declared with var can be redeclared and reassigned.
Example: Declaring variables with var
var name = "Alex";
var name = "Sam"; // Redeclaration OK
name = "Jess"; // Reassignment OK
console.log(name); // "Jess"
javascript
  • Issue: Hoisting (moved to top of scope) can lead to bugs.
console.log(x); // undefined (not an error!)
var x = 5;
javascript
  • Introduced in ECMAScript 6 (ES6, 2015).
  • Block-scoped, can be reassigned but not redeclared in the same scope.
  • let provides better control over variable scoping compared to var
Example: Declaring variables with let
let age = 25;
age = 26; // OK
let age = 30; // Error: Cannot redeclare
console.log(age); // 26
javascript
  • Also introduced in ES6, const is used to declare variables that cannot be reassigned or redeclared after their initial value has been set.
  • const is block scoped.
Example: Declaring variables with const
const pi = 3.14;
pi = 3.15; // Error: Assignment to constant
const person = { name: "Alex" };
person.name = "Sam"; // OK (object mutation)
console.log(person.name); // "Sam"
javascript

Variables are assigned values using the = operator.

  • For const variables, the value must be assigned during declaration and cannot be reassigned afterward.
Example: Assigning value to a JS variable
let name = "Josée"; // Assign a string to 'name'
name = "Bob"; // Reassign the value of "Bob" to 'name'
javascript

The scope of a variable determines where in the code the variable can be accessed.

  • Declared outside functions, accessible everywhere.
let globalVar = "I'm everywhere";
function logIt() {
console.log(globalVar); // "I’m everywhere"
}
logIt();
javascript
  • var is limited to the function it’s declared in.
function test() {
var localVar = "I'm local";
console.log(localVar); // "I’m local"
}
test();
console.log(localVar); // Error: localVar is not defined
javascript
  • let and const are limited to {} blocks (e.g., if, for).
if (true) {
let blockVar = "I'm trapped";
console.log(blockVar); // "I’m trapped"
}
console.log(blockVar); // Error: blockVar is not defined
javascript

// Global scope
let globalVar = "I am global";
function testScope() {
var functionVar = "I am inside the function"; // Function scope
let blockVar = "I am inside a block"; // Block scope
if (true) {
let innerBlockVar = "I am inside an if block"; // Block scope
console.log(innerBlockVar); // Accessible here
}
// console.log(innerBlockVar); // Error: innerBlockVar is not defined outside the if block
}
testScope();
// console.log(functionVar); // Error: functionVar is not defined outside the function
console.log(globalVar); // Accessible globally
javascript