JavaScript – Variables

By Published 2 min read

In JavaScript, variables are fundamental components that allow us to store and manipulate data within our programs. Think of them as containers for holding values, such as numbers, text, or more complex data structures.

Declaration:

When we declare a variable in JavaScript, we’re essentially telling the computer, “Hey, I want to reserve a spot in memory to store something.” We use the var, let, or const keyword to declare variables. For example:

  • var was traditionally used but has some quirks and is now largely replaced by let and const.
  • let is used when we expect the variable to change its value over time.
  • const is used when we want to declare a variable that should never change once it’s assigned a value.

Assigning Values:

After declaring a variable, we can assign a value to it. This value can be a number, a string of text, a boolean (true/false), or even a more complex data type like an array or an object. Here’s how we do it:

   let age = 30;
   let name = "Alice";
   const isStudent = true;Code language: JavaScript (javascript)

In these examples, we’ve declared three variables: age, name, and isStudent, and assigned them values.

Updating Variables:

If we’ve declared a variable with let, we can change its value by simply assigning a new one:

let score = 100;
score = 120; // We updated the value from 100 to 120.Code language: JavaScript (javascript)

Constants:

When we declare a variable with const, it’s like saying, “This value should never change.” Attempting to change it will result in an error:

const pi = 3.14159;
pi = 3.14; // This will cause an error.Code language: JavaScript (javascript)

Variable Naming:

When we choose names for our variables, it’s best to pick descriptive names that help us understand what the variable is for. It’s also a good practice to use camelCase, where the first word starts with a lowercase letter, and subsequent words are capitalized:

let myFavoriteColor = "blue";
let numberOfApples = 10;Code language: JavaScript (javascript)

Scope:

Variables have scope, meaning they are only accessible within the block of code where they’re declared (unless they’re declared in a global scope). For example:

function calculate() {
   let x = 5; // This variable is only accessible within the calculate() function.
   return x * 2;
}Code language: JavaScript (javascript)

Hoisting:

In JavaScript, variable declarations are “hoisted” to the top of their containing function or block, but the assignment does not move. This means you can use a variable before declaring it, but the value will be undefined until it’s assigned.

 console.log(myVariable); // This will not throw an error but will log "undefined."
 var myVariable = 42;Code language: JavaScript (javascript)

Understanding variables is crucial because they allow us to store and work with data dynamically, making our JavaScript programs powerful and flexible.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume