Last modified: May 26, 2026
JS Values & Variables Guide
JavaScript is the language of the web. To write any code, you must understand two core ideas: values and variables. Values are the data. Variables are the containers that hold that data.
This guide will teach you what values and variables are. You will see examples. You will learn best practices. By the end, you will write clean JavaScript code.
What Are Values in JavaScript?
A value is a piece of information. It can be a number, text, or something else. JavaScript has different types of values. These types are called data types.
The most common value types are:
- Number – like 42 or 3.14
- String – text inside quotes, like "Hello"
- Boolean – true or false
- Undefined – a value that has not been set
- Null – intentionally empty value
Values are the building blocks of every program. You use them to perform calculations, store user input, and control logic.
What Are Variables?
A variable is a named container for a value. You create a variable using a keyword like let, const, or var. Then you assign a value to it.
Think of a variable as a labeled box. The label is the variable name. The box holds the value. You can look inside the box anytime.
Declaring Variables
There are three ways to declare a variable in modern JavaScript. Each serves a different purpose.
Using let
Use let when you need to change the value later. It is block-scoped, meaning it only exists inside the curly braces where it is defined.
// Declare a variable with let
let age = 25;
console.log(age); // Output: 25
// You can change the value
age = 26;
console.log(age); // Output: 26
Using const
Use const for values that should never change. It is also block-scoped. Once assigned, you cannot reassign it.
// Declare a constant
const birthYear = 1995;
console.log(birthYear); // Output: 1995
// This will cause an error
// birthYear = 1996; // TypeError: Assignment to constant variable
Using var
var is the old way. It is function-scoped and can be confusing. Avoid using var in new code. Use let and const instead.
// var is older and less predictable
var name = "Alice";
console.log(name); // Output: Alice
For a deeper look at the differences, read our JavaScript Variable Declaration Guide.
Assigning Values to Variables
Assignment is simple. Use the equals sign =. The value on the right goes into the variable on the left.
// Assign a number
let score = 100;
// Assign a string
let greeting = "Hello World";
// Assign a boolean
let isActive = true;
console.log(score); // 100
console.log(greeting); // Hello World
console.log(isActive); // true
You can also assign the result of an expression.
let sum = 5 + 3;
console.log(sum); // 8
Variable Naming Rules
Names must follow strict rules. They cannot start with a number. They can contain letters, digits, underscores, and dollar signs. They are case-sensitive.
- Valid:
myVariable,_count,$price - Invalid:
2fast,my-var,class
Use camelCase for variable names. This means the first word is lowercase, and every following word starts with a capital letter. Example: userAge, totalPrice.
Understanding Data Types
JavaScript is dynamically typed. This means you do not have to declare the type of a variable. The type is determined by the value you assign.
Here are the main data types with examples.
Number
let integer = 10;
let float = 3.14;
console.log(typeof integer); // "number"
console.log(typeof float); // "number"
String
let text = "JavaScript is fun";
console.log(typeof text); // "string"
Boolean
let isLogged = false;
console.log(typeof isLogged); // "boolean"
Undefined
let notDefined;
console.log(notDefined); // undefined
console.log(typeof notDefined); // "undefined"
Null
let empty = null;
console.log(empty); // null
console.log(typeof empty); // "object" (this is a known JavaScript bug)
For a complete list with more details, visit our JavaScript Variable Types Guide.
Variable Scope
Scope determines where a variable is accessible. There are three main scopes: global, function, and block.
Global Scope
Variables declared outside any function or block are global. They can be accessed anywhere in the script.
// Global variable
let globalVar = "I am global";
function showGlobal() {
console.log(globalVar); // Works fine
}
showGlobal(); // I am global
Function Scope
Variables declared inside a function are only accessible inside that function.
function myFunction() {
let localVar = "I am local";
console.log(localVar); // Works
}
myFunction();
// console.log(localVar); // Error: localVar is not defined
Block Scope
let and const are block-scoped. A block is any code between curly braces {}.
if (true) {
let blockVar = "Inside block";
console.log(blockVar); // Works
}
// console.log(blockVar); // Error: blockVar is not defined
Learn more about this topic in our JavaScript Variable Scope Explained article.
Working with Strings and Variables
You often combine strings with variables. Use template literals with backticks for cleaner code.
let firstName = "John";
let lastName = "Doe";
// Old way
let fullName = firstName + " " + lastName;
console.log(fullName); // John Doe
// Modern way with template literals
let greetingMessage = `Hello, ${firstName} ${lastName}!`;
console.log(greetingMessage); // Hello, John Doe!
For more string techniques, see our JavaScript Variable as String Guide.
Common Mistakes with Variables
Beginners often make these errors. Avoid them to write better code.
- Using undeclared variables – always use
letorconst. - Using reserved words like
classorreturnas variable names. - Forgetting that JavaScript is case-sensitive.
myVarandmyvarare different. - Reassigning a
constvariable – this throws an error.
Conclusion
Values and variables are the foundation of JavaScript. Values are the data you work with. Variables are the named containers that store those values.
Use let for values that change. Use const for values that stay the same. Avoid var in modern projects.
Remember the rules for naming variables. Understand scope to avoid bugs. Practice with examples to build confidence.
Now you are ready to write clean JavaScript code. Keep experimenting and learning. The more you practice, the easier it becomes.