JavaScript statements are the building blocks of JavaScript code. They are instructions that tell the browser what to do. Each statement performs a specific action, such as assigning a value, performing a calculation or executing a function.
JavaScript code is executed line by line and statements must follow proper syntax to avoid errors.
Basic Structure of JavaScript Statements
- Code and Syntax: Each JavaScript statement typically ends with a semicolon (
;
). - Whitespace: JavaScript ignores extra spaces, so statements can span multiple lines.
- Case Sensitivity: JavaScript is case-sensitive, so always use correct casing for variable and function names.
Examples of JavaScript Statements
Here are some common types of JavaScript statements with examples:
1. Variable Declaration Statement
Variables are used to store data.
let name = "John"; // Declares a variable and assigns a value
const age = 25; // Declares a constant variable
var city = "London"; // Declares a variable (older syntax)
2. Expression Statements
These are statements that assign or calculate a value.
let sum = 10 + 20; // Adds two numbers and stores the result
let fullName = name + " Doe"; // Combines strings
3. Conditional Statements
Conditional statements allow you to make decisions based on certain conditions.
Example: if…else
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult.");
}
4. Loop Statements
Loop statements execute a block of code multiple times.
Example: for Loop
for (let i = 1; i <= 5; i++) {
console.log("Iteration " + i);
}
5. Function Declaration Statement
Functions are reusable blocks of code that perform a task.
function greet() {
console.log("Hello, welcome to JavaScript!");
}
greet(); // Calls the function
6. Return Statement
The return statement is used in functions to send a value back to the caller.
function addNumbers(a, b) {
return a + b;
}
let result = addNumbers(5, 10); // result is 15
7. Block Statement
Block statements group multiple statements together, enclosed in curly braces {}.
{
let x = 10;
let y = 20;
console.log(x + y); // Outputs 30
}
Important Notes About JavaScript Statements
- Semicolons Are Optional
JavaScript does not require semicolons, but adding them helps prevent errors and makes your code more readable. - Statements vs. Expressions
- A statement performs an action, such as declaring a variable or creating a loop.
- An expression produces a value, such as 5 + 10.
Best Practices for Writing JavaScript Statements
Use Clear and Descriptive Variable Names
Instead of using short or unclear names, choose names that make the purpose of the variable obvious.
let x = 10; // Not clear
let itemCount = 10; // Clear and descriptive
Break Long Statements for Readability
Use line breaks and indentation for better readability.
let message =
"This is a long statement " +
"that spans multiple lines.";
Keep It Simple and Organized
Write statements that are easy to understand and maintain.
Real-Life Example: Using Multiple Statements Together
Here is an example that combines variable declarations, conditionals, and loops.
let students = ["John", "Jane", "Alex", "Emma"]; // Array of students
for (let i = 0; i < students.length; i++) {
if (students[i].startsWith("J")) {
console.log(students[i] + " starts with J.");
} else {
console.log(students[i] + " does not start with J.");
}
}
Output:
John starts with J.
Jane starts with J.
Alex does not start with J.
Emma does not start with J.