Back to JavaScript
2025-12-225 min read

statements, not declarations (JavaScript)

Learn statements, not declarations (JavaScript) step by step with clear examples and exercises.

Title: Mastering Statements and Declarations in JavaScript: A full guide

Why This Matters

In this tutorial, we will delve deep into the world of JavaScript statements and declarations. Understanding these concepts is crucial for writing efficient and error-free code, whether you're building a simple web page or complex applications. It can help you debug real-life coding issues, ace programming interviews, and even tackle challenging coding tasks in your day-to-day work.

By mastering the art of using statements and declarations correctly, you will be able to create more readable, maintainable, and scalable JavaScript code.

Prerequisites

Before diving into statements and declarations, it's essential to have a foundational understanding of JavaScript syntax, variables, data types, control structures (if/else, loops), functions, and basic DOM manipulation. If you're not familiar with these concepts, we recommend reviewing them before proceeding:

Core Concept

JavaScript applications consist of statements with an appropriate syntax. A single statement may span multiple lines. Multiple statements may occur on a single line if each statement is separated by a semicolon (;). This isn't a keyword but a group of keywords, collectively known as statements and declarations.

Statements

Statements are the building blocks of JavaScript programs. They perform actions like declaring variables, assigning values, making decisions, looping through collections, and more. Examples include:

// Assignment statement
var x = 10;
let y = "Hello";

// Conditional statement
if (x > 5) {
console.log("x is greater than 5");
}

// Looping statement
for (let i = 0; i < 10; i++) {
console.log(i);
}

Declarations

Declarations are a specific type of statement used to create variables, functions, and classes. They consist of the keyword (var, let, or const) followed by the identifier (variable name) and an optional initial value. Here's an example:

// Variable declaration
let z = 20;

// Function declaration
function addNumbers(a, b) {
return a + b;
}

// Class declaration
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
}

Worked Example

Let's create a simple JavaScript program that calculates the sum of two numbers and checks if it's even or odd:

// Declare variables for user input
let num1 = prompt("Enter first number");
let num2 = prompt("Enter second number");

// Convert inputs to numbers (since prompt returns strings)
num1 = Number(num1);
num2 = Number(num2);

// Calculate sum and check if it's even or odd
let sum = num1 + num2;
if (sum % 2 === 0) {
console.log(`The sum ${sum} is even`);
} else {
console.log(`The sum ${sum} is odd`);
}

Common Mistakes

1. Forgetting semicolons (;)

In JavaScript, semicolons are optional in most cases, but omitting them can lead to errors. It's best to include them for clarity and consistency.

Example:

// Incorrect: Omit semicolon after function declaration
function add(a, b) return a + b; // SyntaxError: Unexpected token 'return'

// Correct: Include semicolon after function declaration
function add(a, b) { return a + b; }

2. Misunderstanding the difference between statements and declarations

Statements perform actions, while declarations create variables, functions, or classes. Be sure to use the appropriate keyword (var, let, const, function, class) when declaring.

Example:

// Incorrect: Using var instead of let for variable declaration
var x = 10; // Function scope
let y = 20; // Block scope
console.log(x); // Output: 10
console.log(y); // Output: 20

function test() {
console.log(x); // Output: 10 (because x has function scope)
var x = 30; // Re-declare x within the function, overriding its value outside the function
console.log(x); // Output: 30
}
test();
console.log(x); // Output: 10 (because x's value outside the function was not changed)

3. Not defining variables before using them

Always declare your variables before using them in JavaScript. If you attempt to use an undeclared variable, JavaScript will throw a ReferenceError.

Example:

// Incorrect: Attempt to use undeclared variable z
console.log(z); // ReferenceError: z is not defined

let z = 5; // Correct: Declare and assign value to z before using it
console.log(z); // Output: 5

Practice Questions

  1. Write a JavaScript program that calculates the product of two numbers and checks if it's greater than 50.
  2. Declare a function called reverseArray that takes an array as an argument and returns the elements in reverse order.
  3. Create a class called Point with properties x and y. Write a method called distanceFromOrigin that calculates the distance from the origin (0, 0) using the Pythagorean theorem.
  4. Write a JavaScript program that finds the maximum number in an array of numbers.
  5. Create a function called factorial that calculates the factorial of a given number.
  6. Declare a class called Shape with a method area() that returns "Not Implemented" for now.

FAQ

What happens if I forget to include semicolons in my JavaScript code?

JavaScript will still run most of your code without semicolons, but it can lead to unexpected behavior and errors. Including semicolons makes your code easier to read and maintain.

Can I use multiple statements on a single line in JavaScript?

Yes, you can separate multiple statements with semicolons (;) on the same line. However, this practice is generally discouraged because it can make your code harder to read and understand.

What's the difference between var, let, and const in JavaScript?

var has function scope and can be redeclared within the same function; let and const have block scope and cannot be reassigned or redeclared, respectively.

Why is it important to define variables before using them in JavaScript?

Defining variables before using them helps prevent errors caused by attempting to use an undeclared variable. It also makes your code more organized and easier to understand.

statements, not declarations (JavaScript) | JavaScript | XQA Learn