Variables and constants are fundamental concepts in any programming language, including JavaScript. They allow you to store data that can be manipulated or referenced throughout your program. In this section, we will explore the basics of variables and constants in JavaScript, including their declaration, types, scope, and best practices.
Variables in JavaScript are declared using the let keyword for block-scoped variables or the const keyword for constants. You can also initialize a variable at the time of declaration.
// Declare a variable without initialization
let age;
// Declare and initialize a variable
let name = "Alice";
// Declare a constant
const pi = 3.14159;
JavaScript is a dynamically typed language, meaning that the type of a variable can change at runtime. Common types include:
number: integer or floating-point numbersstring: text stringsboolean: boolean values (true or false)object: objects (including arrays and functions)null: null valueundefined: undefined valuelet count = 10;
let pi = 3.14159;
let isDone = true;
let message = "Hello, JavaScript!";
You can declare multiple variables at once using a single statement.
// Using let
let height = 180, weight = 75.5;
// Using const
const length = 20, width = 30;
If a variable is declared but not initialized, it will have a default value of undefined.
let uninitializedVar; // Value is undefined
JavaScript follows camelCase for variable names. Use descriptive names to make your code readable.
// Good practice
userAge = 30;
userName = "JohnDoe";
// Bad practice
uA = 30;
un = "JohnDoe";
Constants are similar to variables, but their values cannot be changed once they are declared. Use the const keyword for constants.
Constants can be declared using the const keyword followed by the constant name and value.
// Declare a constant
const Pi = 3.14159;
// Multiple constants declaration
const MaxAge = 100, MinAge = 0;
Constants can be of any type, including strings, numbers, booleans, and objects.
const Name = "JavaScript";
const Version = 1.18;
const IsSupported = true;
const UnicodeChar = 'A';
const for values that do not change: Constants are useful for defining configuration settings, error messages, and other fixed values.Variables and constants have different scopes depending on where they are declared:
Variables declared inside a block (e.g., within a function, loop, or if statement) using let or const are only accessible within that block.
function main() {
let x = 10;
if (true) {
let y = 20;
console.log(x + y); // Accessible
}
console.log(x); // Accessible
// console.log(y); // Error: y is not defined
}
Variables declared with var inside a function are accessible throughout the entire function.
function main() {
var globalVar = 100;
if (true) {
var localVar = 200;
console.log(localVar); // Accessible
}
console.log(globalVar); // Accessible
console.log(localVar); // Accessible
}
Variables declared outside any function using var are accessible throughout the entire script.
var globalVar = 100;
function main() {
console.log(globalVar);
}
function anotherFunction() {
console.log(globalVar);
}
JavaScript supports type inference, allowing you to declare variables without explicitly specifying their types. The engine will infer the type based on the assigned value.
let inferredInt = 42; // number
let inferredFloat = 3.14; // number
let inferredString = "Hello"; // string
However, this feature is limited to local variables and cannot be used for constants or global variables.
Variables and constants are essential components of any JavaScript program. Understanding how to declare, initialize, and use them effectively will help you write clean, efficient, and maintainable code. By following best practices such as using descriptive names and avoiding global variables, you can improve the readability and reliability of your applications.
In the next section, we will explore control structures in JavaScript, including loops and conditional statements, which allow you to execute different blocks of code based on certain conditions.