Back to JavaScript
2026-03-218 min read

Difference between statements and declarations (JavaScript)

Learn Difference between statements and declarations (JavaScript) step by step with clear examples and exercises.

Why This Matters

In this extensive guide, we will delve deep into the significant difference between statements and declarations in JavaScript, providing practical examples, real-life scenarios, and common mistakes to help you master this crucial aspect of programming. Understanding the distinction between these two concepts is vital for writing clean, efficient, and error-free JavaScript code. Proper usage can prevent runtime errors, improve readability, and make your code more maintainable. Additionally, grasping this concept will help you tackle real-world programming challenges and prepare for interviews or exams.

Prerequisites

Before diving into statements and declarations, ensure you have a good understanding of the following concepts:

  1. Basic JavaScript syntax (variables, operators, expressions)
  2. Control structures (if/else, loops)
  3. Functions
  4. Objects and arrays
  5. Understanding the concept of scope in JavaScript
  6. Familiarity with ES6 features like let and const
  7. Comprehension of variable hoisting and function hoisting
  8. Knowledge of common JavaScript errors and debugging techniques
  9. A solid understanding of data types and type coercion in JavaScript
  10. Experience working with browser development tools such as the console, debugger, and network tab

Core Concept

Statements

In JavaScript, statements are individual instructions that perform an action or a series of actions. They are executed sequentially, one after another, until the end of the script block or function. A single statement may span multiple lines if it is enclosed in curly braces {}. Multiple statements can also be combined on a single line using semicolons (;) as separators.

Here's an example of various types of JavaScript statements:

// Variable declaration statement
let x = 10;

// Assignment statement
x += 5;

// Conditional statement
if (x > 15) {
console.log("x is greater than 15");
} else if (x === 20) {
console.log("x equals 20");
} else {
console.log("x is less than 15 or not equal to 20");
}

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

// Function declaration statement
function greet() {
console.log("Hello, World!");
}

// Function expression statement
const add = function(a, b) {
return a + b;
};

Declarations

Declarations are special types of statements used to create variables, functions, and classes in JavaScript. They provide a way to define new entities within the scope of your code. Each declaration consists of a keyword (let, const, function, or class) followed by an identifier (name) and optional initial value or function body.

// Variable declaration statement using let
let y = 20;

// Function declaration statement
function multiply(a, b) {
let result = a * b;
console.log("Inside multiply():", result); // Output: Inside multiply(): 400 (for a=10, b=40)
}

// Class declaration statement
class MyClass {
constructor() {
this.property = "Hello";
}
}

Block Statements

A block statement consists of one or more statements enclosed within curly braces {}. It is treated as a single statement and can be used to group related statements together. For example:

// Example of a block statement
{
let x = 10;
console.log(x); // Output: 10
}

// Block statement with multiple statements
{
let y = 20;
let z = y * 2;
console.log(z); // Output: 40
}

Worked Example

Let's work through an example that demonstrates the difference between statements and declarations, as well as block statements:

// Declare a variable x with initial value 10
let x = 10;
console.log(x); // Output: 10

// Declare a function sum()
function sum(a, b) {
let result = a + b;
console.log("Inside sum():", result); // Output: Inside sum(): 25 (for a=10, b=15)
}

// Call the sum() function with arguments 10 and 15
sum(10, 15);

// Declare a block statement with multiple statements
{
let y = 20;
let z = y * 2;
console.log("Inside block:", z); // Output: Inside block: 40
}

// Accessing y outside the block will result in an error because it is not hoisted and has block scope
// console.log(y); // ReferenceError: y is not defined

// Declare a variable z using const
const z = "World";
console.log(z); // Output: World

// Assign a new value to the z variable (this will result in an error because z is declared with 'const')
// z = "JavaScript"; // TypeError: Assignment to constant variable.

Common Mistakes

1. Forgetting semicolons

JavaScript automatically inserts semicolons at the end of statements, but it's still a good practice to include them explicitly. Failing to do so can lead to unexpected behavior and errors.

// Correct usage
let x = 10;
console.log(x);

// Incorrect usage (missing semicolon)
let x = 10
console.log(x); // SyntaxError: Unexpected token =

2. Variable hoisting

Variables declared with var, let, or const are hoisted to the top of their current scope, but they are not initialized until they are actually used. This can lead to unexpected behavior when variables are accessed before being declared.

// Incorrect usage (variable is accessed before declaration)
console.log(x); // undefined
let x = 10;

// Correct usage (variable is declared before accessing it)
let x;
console.log(x); // undefined
x = 10;

3. Function hoisting

Function declarations are hoisted to the top of their current scope, but function expressions are not. This can lead to unexpected behavior when functions are called before they are defined.

// Incorrect usage (function expression is called before definition)
console.log(sum(10, 15)); // ReferenceError: sum is not defined
let mySum = function(a, b) {
return a + b;
};

// Correct usage (function declaration is hoisted and can be called immediately)
function sum(a, b) {
return a + b;
}
console.log(sum(10, 15)); // Output: 25

4. Using var instead of let or const

Using the var keyword to declare variables can lead to issues with function scope and variable hoisting. It's recommended to use let or const for better control over variable scoping and behavior.

// Incorrect usage (using var)
function test() {
var x = 10;
console.log(x); // Output: 10
}

test();
console.log(x); // Output: 10 (x is accessible outside the function because of function scope with 'var')

Practice Questions

  1. What is the difference between statements and declarations in JavaScript?
  2. What happens when you forget to include semicolons in your JavaScript code?
  3. Why does variable hoisting occur in JavaScript, and how can it lead to unexpected behavior?
  4. What is the difference between function declarations and function expressions in JavaScript?
  5. In the following code snippet, what will be the output of console.log(x)?
let x;
console.log(x); // undefined
x = 10;
  1. What is a block statement in JavaScript, and how can it be used to group related statements together?
  2. Why is it important to include semicolons explicitly in your JavaScript code, even though the interpreter automatically inserts them?
  3. What are some common mistakes that developers make when working with variables and functions in JavaScript, and how can these mistakes be avoided?
  4. How does the use of let or const differ from using var in terms of variable hoisting and scope?
  5. In the following code snippet, what will be the output of console.log(y)?
function test() {
let y = 20;
}

test();
console.log(y); // ReferenceError: y is not defined

FAQ

1. Why does JavaScript automatically insert semicolons at the end of statements?

JavaScript automatically inserts semicolons to maintain compatibility with older versions of the language, which did not include automatic semicolon insertion (ASI). Although modern browsers have improved ASI, it's still a good practice to include semicolons explicitly for clarity and consistency.

2. What is the difference between var, let, and const in JavaScript?

var has function-level scope and can be reassigned or redeclared within its scope. let and const have block-level scope (they are not hoisted) and cannot be redeclared, but let can be reassigned. const variables must be initialized at the time of declaration and cannot be reassigned thereafter.

3. Can I use multiple variable declarations on a single line using semicolons?

Yes, you can declare multiple variables on a single line using semicolons. However, this practice is generally discouraged because it can make your code harder to read and maintain.

let x = 10, y = 20, z = 30;

4. Is there any difference in behavior between var, let, and const when used within a function?

Yes, there is a difference in behavior when using var, let, and const within a function. Variables declared with var have function scope, while variables declared with let or const have block scope (they are not hoisted). Additionally, reassigning a variable declared with let or const will modify its value, whereas reassigning a variable declared with var within the same function will create a new variable with the same name.

function test() {
var x = 10; // Function scope (x can be accessed outside the function)
let y = 20; // Block scope (y cannot be accessed outside the block)
const z = 30; // Block scope (z cannot be reassigned or redeclared within the block)

console.log(x); // Output: 10
console.log(y); // Output: 20
console.log(z); // Output: 30

x = 25; // Modifying x with 'var' creates a new variable and does not affect the original one (outside the function)
console.log(x); // Output: 10 (original value outside the function remains unchanged)
}

5. What is the purpose of using block statements in JavaScript, and how can they improve your code's readability and maintainability?

Block statements allow you to group related statements together within curly braces {}. This makes it easier to visually identify a collection of statements that belong together, improves code readability, and helps prevent errors caused by accidentally modifying variables outside the intended scope. By using block statements, you can also create new scopes for variables, which can help manage variable declarations and avoid unintended side effects.

6. Why is it important to understand the difference between statements and declarations in JavaScript?

Understanding the distinction between statements and declarations is crucial for writing clean, efficient, and error-free JavaScript code. Statements are individual instructions that perform actions, while declarations create new variables, functions, or classes within your code. Proper usage can prevent runtime errors, improve readability, and make your code more maintainable. Additionally, grasping this concept will help you tackle real-world programming challenges and prepare for interviews or exams.

7. What are some common mistakes that developers make when working with variables in JavaScript, and how can these mistakes be avoided?

Some common mistakes include forgetting to declare variables before using them, using var instead of let or const, accidentally modifying variables outside their intended scope, and forgetting to include semicolons. To avoid these mistakes, it's important to follow best practices such as declaring variables at the top of their respective scopes, using let or const for better control over variable scoping and behavior, and including semicolons explicitly for clarity and consistency.

8. What is the difference between function declarations and function expressions in JavaScript?

Function declarations are defined using

Difference between statements and declarations (JavaScript) | JavaScript | XQA Learn