JavaScript Tutorial
JavaScript Tutorial
JS Statements
JavaScript statements are instructions that the browser executes from top to bottom. Every action in JavaScript—like declaring variables, performing operations, or calling functions—is written as a statement.
What Are JavaScript Statements?
A JavaScript statement is a piece of code that performs an action.
Just like sentences in English end with a period, JavaScript statements usually end with a semicolon (;).
The browser reads and executes statements one by one, from top to bottom.
Example:
let x = 5; // Statement 1
let y = 10; // Statement 2
let z = x + y; // Statement 3Each line above is a separate statement.
Semicolons in JavaScript
Semicolons are used to separate JavaScript statements.
let name = "John";
let age = 25;
JavaScript can still run without semicolons in many cases, but using them is a good practice and prevents unexpected errors.
Multiple Statements on One Line
You can also write multiple statements on one line:
let a = 1; let b = 2; let c = a + b;
This is valid but not recommended for readability.
Blocks of Statements
JavaScript groups statements inside curly braces { }.
These are mostly used in functions, loops, and conditionals.
Example:
if (age >= 18) {
console.log("You are an adult");
alert("Welcome!");
}
The code inside { } is called a code block.
JavaScript Keywords
Some statements start with keywords, such as:
Keyword | Description |
|---|---|
| Declares a block-scoped variable |
| Declares a constant |
| Declares a function-scoped variable |
| Creates a condition |
| Creates a loop |
| Defines a function |
| Returns a value |
Example:
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
}
Whitespace and Line Breaks
JavaScript ignores extra spaces and new lines, so you can format your code for better readability.
These two examples behave the same:
let x = 10 + 5;
let x =
10 +
5;
Readable code is always better.
Keep statements on separate lines for readability.
Use code blocks { } to group statements inside functions or conditionals.
<script>
document.getElementById("elem").innerHTML = "Hello World!";
</script>
JavaScript Statements
JavaScript programs consist of statements with appropriate syntax.
Asingle JavaScript statement may span a single or multiple lines.
JavaScript statements should be ended or be separated by semicolons ( ; ).
Below is an example of a single-line statement.
Below is an example of a single-line statement.
This statement writes the text "Hello World!" to the paragraph element with the elem id.
single-line statement Example
<!DOCTYPE html>
<html>
<head>
<title> Try It Yourself </title>
</head>
<body>
<p id="elem"> First Paragraph Element </p>
<script>
document.getElementById("elem").innerHTML = "Hello World!";
</script>
</body>
</html>
Explanation
In the example:
let x = 5; and let y = 7; are variable declaration statements.
The function addNumbers() block contains multiple statements grouped together.
Inside the function, let result = x + y; calculates the sum, and console.log() displays it.
Finally, addNumbers(); calls the function, which is also a statement.