Values and Variables
JavaScript Values and Variables
Introduction
In JavaScript, values represent the data that you work with, and variables are containers that hold these values. Understanding how to declare and use variables is fundamental to writing JavaScript code.
Values
Values are the pieces of data that you can manipulate in JavaScript. They can be of various data types, including:
- Numbers: Integers and floating-point numbers (e.g.,
42
,3.14
) - Strings: Text enclosed in quotes (e.g.,
'Hello, World!'
,"JavaScript"
) - Booleans: Logical values that can be
true
orfalse
- Objects: Complex data structures that can hold multiple values (e.g.,
{ name: 'Alice', age: 25 }
) - Arrays: Ordered collections of values (e.g.,
[1, 2, 3, 4, 5]
) - Null: A special value that represents “no value” or “empty”
- Undefined: A value that indicates a variable has been declared but not assigned a value
Variables
Variables are used to store values so that they can be reused and manipulated throughout your code. You can think of variables as labeled boxes that hold data.
Declaring Variables
In JavaScript, you can declare variables using three keywords: var
, let
, and const
.
- var: The
var
keyword was used in the past to declare variables, but it has been mostly replaced bylet
andconst
due to scope and hoisting issues.
var message = 'Hello, World!';
let: The let
keyword is used to declare block-scoped variables. It’s the preferred way to declare variables that can be reassigned.
let age = 25;
age = 26; // Reassigning the value of the variable
const: The const
keyword is used to declare block-scoped variables that cannot be reassigned. It’s the preferred way to declare variables that should not change.
const pi = 3.14159;
// pi = 3.14; // This will cause an error because the value cannot be changed
Naming Variables
Variable names (also called identifiers) must follow certain rules:
- They can contain letters, digits, underscores, and dollar signs.
- They must begin with a letter, underscore, or dollar sign.
- They are case-sensitive (e.g.,
myVariable
andmyvariable
are different). - They cannot be reserved keywords (e.g.,
let
,const
,function
, etc.).
Examples
Here are some examples of declaring and using variables:
// Declaring variables using let
let name = 'Alice';
let age = 30;
let isStudent = true;
// Declaring a constant variable using const
const pi = 3.14159;
// Printing values to the console
console.log(name); // Output: Alice
console.log(age); // Output: 30
console.log(isStudent); // Output: true
console.log(pi); // Output: 3.14159
// Reassigning values to variables declared with let
age = 31;
console.log(age); // Output: 31
// Attempting to reassign a value to a constant variable will cause an error
// pi = 3.14; // Uncaught TypeError: Assignment to constant variable.
Variable Scope
The scope of a variable determines where it can be accessed in your code. JavaScript has function scope and block scope.
- Function Scope: Variables declared with
var
inside a function are scoped to that function and cannot be accessed outside of it. - Block Scope: Variables declared with
let
orconst
inside a block (e.g., inside{}
) are scoped to that block and cannot be accessed outside of it.
Example of Scope
function exampleFunction() {
var functionScoped = 'I am function scoped';
if (true) {
let blockScoped = 'I am block scoped';
console.log(functionScoped); // Accessible
console.log(blockScoped); // Accessible
}
console.log(functionScoped); // Accessible
// console.log(blockScoped); // Uncaught ReferenceError: blockScoped is not defined
}
exampleFunction();
Summary
- Values are the data you work with in JavaScript, and they can be of various types such as numbers, strings, booleans, objects, arrays, null, and undefined.
- Variables are containers that hold values and can be declared using
var
,let
, orconst
. var
is function-scoped, whilelet
andconst
are block-scoped.let
is used for variables that can be reassigned, whileconst
is used for variables that should not change.
By mastering values and variables, you’ll be well-equipped to handle data and write effective JavaScript code.