Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - JavaScript Variables


JavaScript Variables

In this chapter, you will learn how to store data using variables.

Variables hold values that your scripts can use, display, or change later.


What is a Variable?

A variable is a container for storing data values. You can think of variables as labeled boxes that hold information.

You can create a variable with the let keyword, followed by the name of the variable and a value:

Example

Declaring variables:

let name = "John";
let age = 50;
Try it Yourself »

Note: Text values are written inside quotes, while numbers are written without quotes.


Declaring Variables

In JavaScript, you can declare variables with:

  • let - for variables that can change
  • const - for variables that should not change
  • var - the old way (avoid in modern code)

Example

Using let and const:

let color = "blue";
color = "green"; // allowed

const pi = 3.14;
// pi = 4; // not allowed - const cannot be reassigned
Try it Yourself »

Variable Naming Rules

Variable names must follow these rules:

  • Must start with a letter, _, or $
  • Can contain letters, numbers, underscores, or dollar signs
  • Cannot start with a number
  • Cannot use reserved keywords (like let or for)

Example

Valid and invalid names:

// Valid
let firstName = "John";
let _count = 5;
let $price = 10;

// Invalid
// let 2cool = "no";
// let let = 4;
Try it Yourself »

Reassigning Variables

Variables declared with let can be reassigned to a new value.

Example

Reassigning a value:

let mood = "happy";
mood = "sad";
Try it Yourself »

Constants (const)

Use const for values that should not change during the program.

Example

Using const:

const siteName = "W3Schools";
const yearFounded = 1998;
Try it Yourself »

Note: A constant cannot be changed after it is created.


Multiple Declarations

You can declare multiple variables in one statement, separated by commas.

Example

Declaring multiple variables:

let a = 1, b = 2, c = 3;
console.log(a + b + c);
Try it Yourself »

Data Types

A variable can store different kinds of data. Each type of data is used for a different purpose:

TypeExampleDescription
String"Hello"Text inside quotes
Number42Numbers with or without decimals
Booleantrue or falseYes/No values
Object{name: "Bo", age: 25}Data stored in key-value pairs
Array["HTML", "CSS", "JS"]List of values inside brackets
Undefinedlet x;Declared but not given a value

Note: Text values are written inside quotes. Numbers and Boolean values (true and false) are written without quotes.


Dynamic Typing

JavaScript is dynamically typed - this means a variable can hold different types of data at different times.

Example

A variable can change its type while the program runs:

let x = 5; // number
x = "five"; // now string
Try it Yourself »

Note: In JavaScript, you do not need to tell the browser what type a variable is. The type is decided automatically when you assign a value, and it can change if the value changes later.


Best Practices

  • Use const by default; use let only when you need to reassign.
  • Avoid var in modern code.
  • Use descriptive names: userName is better than x.
  • Initialize variables when you declare them.

Next, you will learn about JavaScript Operators and Expressions - how to perform calculations, compare values, and combine data.

Next » JavaScript Operators and Expressions


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.