Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript Statements
Next article icon

JavaScript Control Flow Statements

Last Updated : 07 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Control flow statements in JavaScript control the order in which code is executed. These statements allow you to make decisions, repeat tasks, and jump between parts of a program based on specific conditions.

JavaScript if Statement

The if statement executes a block of code only if a specified condition is true.

JavaScript
const age = 18; if (age >= 18) {     console.log("You are an adult."); } 

Output
You are an adult. 
  • Checks if age is greater than or equal to 18.
  • Logs "You are an adult." if the condition is true.

JavaScript if...else Statement

The if...else statement provides an alternate block of code to execute if the condition is false.

JavaScript
const score = 40; if (score >= 50) {     console.log("You passed."); } else {     console.log("You failed."); } 

Output
You failed. 
  • It will log "You passed." if the score is 50 or more.
  • Otherwise, logs "You failed."

JavaScript if...else if...else Statement

The if...else if...else statement is used when you want to handle multiple conditions.

JavaScript
const temp = 25; if (temp > 30) {     console.log("It's hot."); } else if (temp >= 20) {     console.log("It's warm."); } else {     console.log("It's cold."); } 

Output
It's warm. 
  • Checks if the temperature is greater than 30, logs "It's hot."
  • If not, checks if it's between 20 and 30, logs "It's warm."
  • Otherwise, logs "It's cold."

JavaScript switch Statement

The switch statement evaluates an expression and executes a block of code based on matching cases. It provides an alternative to long if-else chain.

JavaScript
const day = "Monday"; switch (day) {     case "Monday":         console.log("Start of the week.");         break;     case "Friday":         console.log("End of the workweek.");         break;     default:         console.log("It's a regular day."); } 

Output
Start of the week. 
  • Checks the value of day and matches it to a case.
  • Logs "Start of the week." if day is "Monday".
  • Logs "End of the workweek." if day is "Friday".
  • Logs "It's a regular day." if no cases match.

JavaScript Ternary Operator or Conditional Operator:

In some programming languages, a ternary operator is used to assign a value to a variable based on a condition.

JavaScript
let a = 10; console.log(a === 5 ? "a is equal to 5" : "a is not equal to 5"); 

Output

a is not equal to 5
  • Variable Declaration: let a = 10; assigns 10 to variable a.
  • Ternary Operator: a === 5 ? "a is equal to 5" : "a is not equal to 5"; checks if a is strictly equal to 5.
    • If true, it returns "a is equal to 5".
    • If false, it returns "a is not equal to 5".

Uses of Control Flow Statements

Control flow statements are backbone in programming for

  • Decision-Making: To execute specific blocks of code based on conditions (e.g., if, if...else).
  • Branching: To exit loops or skip iterations (break, continue).
  • Looping: To repeat tasks (for, while, do...while).
  • Switching: To handle multiple conditions effectively (switch).

Next Article
JavaScript Statements

S

souravsharma098
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-basics

Similar Reads

    JavaScript- Control Flow Statements
    Control flow statements in JavaScript control the order in which code is executed. These statements allow you to make decisions, repeat tasks, and jump between parts of a program based on specific conditions.JavaScript if StatementThe if statement executes a block of code only if a specified conditi
    3 min read
    JavaScript Statements
    JavaScript statements are programming instructions that a computer executes. A computer program is essentially a list of these "instructions" designed to perform tasks. In a programming language, such instructions are called statements.Types of Statements1. Variable Declarations (var, let, const)In
    4 min read
    JavaScript Control Flow Coding Practice Problems
    Control flow structures like conditionals and loops design how JavaScript programs execute. Mastering if-else statements, switch cases, loops, and recursion is important for writing efficient code. This curated list of JavaScript control flow practice problems covers a variety of exercises to help y
    1 min read
    How "Control Flow" Controls the Functions Calls ?
    Control flow is a fundamental concept in programming that determines the order in which code statements and function calls are executed. By controlling the flow of a program, you can create complex logic, handle different execution paths, and manage the sequence of function calls. Understanding cont
    11 min read
    Control flow statements in Programming
    Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow St
    15+ min read
    Java if statement
    The Java if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e. if a certain condition is true then a block of statements is executed otherwise not.Example:Java// Java program to illustrate If st
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences