-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
In JavaScript, statements are the building blocks of the code. They are instructions that the browser executes to perform specific actions, such as declaring variables, performing calculations, controlling program flow, or interacting with the DOM. Understanding statements is essential because they form the logic of any JavaScript program. Every JavaScript program is essentially a sequence of statements executed step by step.
In this chapter, you will learn what JavaScript statements are, their types, syntax rules, practical examples, common mistakes, best practices, and real-world applications.
A JavaScript statement is a complete instruction that performs an action. Statements can be simple, like assigning a value to a variable, or complex, like loops and conditional statements. Each statement typically ends with a semicolon (;), though JavaScript allows optional semicolons in many cases due to automatic semicolon insertion.
let name = "Alice"; // Variable declaration statement
console.log(name); // Output statement
Here, the first statement declares a variable, and the second statement logs its value to the console.
Declaration statements define variables or constants. JavaScript uses var, let, and const for declarations.
let age = 25; // let declaration
const country = "India"; // const declaration
var city = "Delhi"; // var declaration
let is block-scoped and can be reassigned.
const is block-scoped and cannot be reassigned.
var is function-scoped and can lead to unexpected behavior in modern code, so it is less recommended.
Expression statements evaluate an expression and optionally assign a value.
let total = 10 + 5; // Expression statement performing addition
Here, 10 + 5 is evaluated, and the result is assigned to the variable total.
Conditional statements control the flow of the program based on conditions. JavaScript uses if, else if, else, and switch for conditional logic.
let score = 85;
if (score >= 90) {
console.log("Grade A");
} else if (score >= 75) {
console.log("Grade B");
} else {
console.log("Grade C");
}
This evaluates the score and prints the appropriate grade. Conditional statements are fundamental for decision-making in programs.
Loops are statements that repeatedly execute a block of code until a condition is met. Common loops include for, while, and do...while.
for (let i = 1; i <= 5; i++) {
console.log("Iteration " + i);
}
This for loop prints numbers 1 through 5. Loops help automate repetitive tasks efficiently.
Jump statements change the normal flow of execution, including break, continue, and return.
for (let i = 1; i <= 5; i++) {
if (i === 3) continue; // Skip iteration when i = 3
console.log(i);
}
Here, continue skips printing 3, demonstrating flow control.
Block statements group multiple statements inside curly braces {}. They are often used with loops and conditionals.
if (true) {
let x = 10;
console.log(x);
}
Blocks help organize code logically and prevent scope-related issues.
An empty statement (;) does nothing. It can be used to satisfy syntax requirements, such as in loops with no body.
for (let i = 0; i < 5; i++);
Here, the loop executes five times but has no operations inside the body.
Most statements end with a semicolon (;), although JavaScript often inserts them automatically.
Statements can span multiple lines for readability.
Comments can be added using // for single-line or /* */ for multi-line.
Statements can be nested inside blocks to form complex logic.
let name = "Alice";
let age = 20;
if (age >= 18) {
console.log(name + " is an adult.");
} else {
console.log(name + " is a minor.");
}
for (let i = 1; i <= 3; i++) {
console.log("Welcome message " + i);
}
This example uses variable declarations, a conditional statement, and a loop together to demonstrate typical program flow.
Ensure that output from JavaScript statements is accessible (e.g., dynamic content updates should be readable by screen readers)
Avoid excessive reliance on alert() for notifications
Maintain predictable program flow for assistive technologies
Omitting semicolons in situations where they are required
Misusing var instead of let or const
Using assignment = instead of comparison == or === in conditions
Forgetting to close blocks with curly braces
Over-nesting statements, making code unreadable
Avoiding these mistakes leads to cleaner, maintainable, and bug-free code.
Use let and const instead of var for modern, safer code
Keep statements simple and readable
Group related statements inside blocks for clarity
Comment code to describe the purpose of complex statements
Test conditional and loop statements thoroughly to avoid logic errors
Minimize global variables and use block-scoped statements
Following these practices ensures code quality and maintainability.
JavaScript statements are used in almost every aspect of web development:
Variable declarations and data storage
Conditional rendering of content based on user actions
Loops for displaying lists, tables, or dynamic menus
Jump statements for controlling program flow in forms or applications
Grouping statements in functions and blocks for modular design
Mastering statements is essential for building interactive and reliable web applications.
JavaScript statements are the fundamental units of code that execute instructions in a program. They include declaration, expression, conditional, loop, jump, block, and empty statements. Understanding their types, syntax, and proper usage is crucial for controlling program flow, handling user input, and manipulating webpage content. Best practices, accessibility considerations, and avoiding common mistakes ensure clean, maintainable, and effective code.
Q1. How do you declare a variable price with a value of 100 in JavaScript?
Q2. How can you write a JavaScript statement to assign the sum of two variables x and y to a third variable total?
Q3. How do you write a conditional statement that checks if a variable age is greater than 18 and logs "Adult" if true?
Q4. How do you write a loop that prints numbers from 1 to 5 using a for loop statement?
Q5. How do you write a function call statement that displays "Welcome!" using the alert() method?
Q6. How do you group two or more statements inside a code block using {} in JavaScript?
Q7. How do you create a constant variable named PI with the value 3.14?
Q8. What is the correct way to write multiple statements on separate lines with semicolons? Provide an example.
Q9. How do you write a statement that multiplies two numbers and stores the result in a variable named product?
Q10. How do you declare a variable status, assign it a string "active", and log it to the console?
Finished learning? Play this simple quiz to reinforce what you just studied.
Write and run code directly in your browser. Live preview for frontend languages.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
