Home > Web Front-end > JS Tutorial > JavaScript Variables: let vs const vs var

JavaScript Variables: let vs const vs var

Susan Sarandon
Release: 2025-01-11 16:28:42
Original
113 people have browsed it

JavaScript Variables: let vs const vs var

JavaScript Simplified: A Beginner's Guide to Mastering Interactive Web Development

Post 4 of 30: Understanding Variables in JavaScript

In this post, we’ll explore what variables are, how to declare them, and how they work in JavaScript.


What is a Variable?

A variable is a way to store a value in JavaScript so we can use it later. Think of a variable like a labeled box where you can keep things and retrieve them when needed.

For example, instead of writing "John" multiple times in your code, you can store it in a variable and use it anywhere.


Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  • var (older method, not recommended)
  • let (modern and recommended for changeable values)
  • const (for values that should not change)

1. Using let (Recommended)

let name = "John";
console.log(name);
Copy after login
Copy after login

Output:

John
Copy after login
Copy after login

Here, we:

  • Created a variable called name
  • Assigned it the value "John"
  • Used console.log() to print the value of name

2. Using const (For Constant Values)

const PI = 3.1416;
console.log(PI);
Copy after login
Copy after login

Output:

3.1416
Copy after login
Copy after login
  • const is used for values that should not change.
  • Once assigned, you cannot reassign a new value to PI.

3. Using var (Older Method – Avoid Using)

var age = 25;
console.log(age);
Copy after login

Output:

25
Copy after login
  • var was commonly used before let and const, but it has scoping issues, so use let or const instead.

Changing Variable Values

With let, you can change a variable’s value, but with const, you cannot.

Example with let:

let city = "New York";
console.log(city); // Output: New York

city = "Los Angeles"; // Changing the value
console.log(city); // Output: Los Angeles
Copy after login

Example with const (This will cause an error)

const country = "USA";
console.log(country);

country = "Canada"; // ❌ This will cause an error
console.log(country);
Copy after login

Error: Uncaught TypeError: Assignment to constant variable.


Variable Naming Rules

When naming variables, follow these rules:

✔️ Can contain letters, numbers, $, and _

✔️ Must start with a letter, $, or _ (not a number)

✔️ Case-sensitive (name and Name are different)

✔️ Cannot be a reserved keyword (like let, console, function, etc.)

Examples of Valid Variable Names:

let name = "John";
console.log(name);
Copy after login
Copy after login

Examples of Invalid Variable Names:

John
Copy after login
Copy after login

Practical Exercise: Storing and Changing Values

Try this in your script.js file:

const PI = 3.1416;
console.log(PI);
Copy after login
Copy after login

Expected Output:

3.1416
Copy after login
Copy after login

Next Steps

Now that we understand how variables work, the next step is to explore data types in JavaScript—including numbers, strings, booleans, and more!

Stay tuned for the next post! ?


Pro Tip:

? Use let when you expect the value to change.

? Use const when the value should stay the same.

? Avoid var unless you specifically need it.

Follow me on LinkedIn - Ridoy Hasan
Visit my website - Ridoyweb
*Visit my agency website *- webention digital

The above is the detailed content of JavaScript Variables: let vs const vs var. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template