Skip to main content

JavaScript Control Statements | if - if-else if-else Statement - switch Statement in JavaScript

JavaScript Control Statements:

JavaScript control statements are used to manage the flow of a program by making decisions and repeating actions based on conditions. The primary control statements in JavaScript include:

JavaScript Control Statements - if - if-else if-else Statement - switch Statement in JavaScript
JavaScript Control Statements

1. if Statement:

   - The `if` statement is used to execute a block of code if a specified condition evaluates to true.
   javascript
   if (condition) {
     // Code to be executed if the condition is true
   }

2. if-else Statement:

   - The `if-else` statement is used to execute one block of code if a condition is true and another block if the condition is false.
   javascript
   if (condition) {
     // Code to be executed if the condition is true
   } else {
     // Code to be executed if the condition is false
   }

3. if-else if-else Statement:

   - This statement allows you to test multiple conditions and execute different blocks of code based on which condition is true.
   javascript
   if (condition1) {
     // Code to be executed if condition1 is true
   } else if (condition2) {
     // Code to be executed if condition2 is true
   } else {
     // Code to be executed if none of the conditions are true
   }

4. switch Statement:

   - The `switch` statement is used to perform different actions based on different conditions.
   javascript
   switch (expression) {
     case value1:
       // Code to be executed if expression matches value1
       break;
     case value2:
       // Code to be executed if expression matches value2
       break;
     default:
       // Code to be executed if none of the cases match
   }

5. for Loop:

   - The `for` loop is used to iterate over a block of code a specified number of times.
   javascript
   for (initialization; condition; iteration) {
     // Code to be executed in each iteration
   }
   

6. while Loop:

   - The `while` loop is used to repeatedly execute a block of code as long as a specified condition is true.
   javascript
   while (condition) {
     // Code to be executed while the condition is true
   }
   

7. do-while Loop:

   - The `do-while` loop is similar to the `while` loop, but it ensures that the code block is executed at least once before checking the condition.
   javascript
   do {
     // Code to be executed at least once
   } while (condition);
   

8. for...in Loop:

   - The `for...in` loop is used to iterate over the properties of an object.
   javascript
   for (variable in object) {
     // Code to be executed for each property of the object
   }
   

9. for...of Loop:

   - The `for...of` loop is used to iterate over the values of an iterable object (arrays, strings, etc.).
   javascript
   for (variable of iterable) {
     // Code to be executed for each value of the iterable
   }
   

These control statements provide the foundation for managing program flow and implementing various logic structures in JavaScript. They are essential for creating dynamic and responsive applications.

Comments

Popular posts from this blog

Biography of Manohar Joshi

Biography of Manohar Joshi: Manohar Gajanan Joshi is an Indian politician who has played a significant role in Maharashtra state politics. Born on December 2, 1937, in a middle-class family in a village called Ganeshpeth in Ratnagiri district, Maharashtra, Joshi's journey into politics was marked by his association with the Shiv Sena, a right-wing regional political party in India. Joshi's political career began in the late 1960s when he joined the Shiv Sena, which was then led by its founder, Bal Thackeray. He quickly rose through the ranks of the party due to his organizational skills, dedication, and ideological alignment with the party's principles, which advocated for the interests of the Marathi-speaking population of Maharashtra. In 1972, Manohar Joshi won his first election to the Brihanmumbai Municipal Corporation (BMC), marking the beginning of his electoral career. He served as a corporator in the BMC and later became the Mayor of Mumbai in 1976. Joshi's tenu...

Best Money Earning Apps in India 2026 – Online Jobs & Income Guide

Best Apps to Earn Money Online in India (2026 Guide) Introduction With the rapid growth of Technology and digital platforms, earning Money online in India has become easier than ever. From students to professionals, everyone is exploring online Jobs and Business opportunities using mobile apps. In this blog, we will explore the Best Apps to Earn Money Online in India , along with selection process, benefits, earning power, and latest developments. Why Choose Money Earning Apps? Online earning apps are becoming popular due to: Flexible working hours No high investment required Easy to start with basic skills Opportunity to build a scalable Business Increasing demand due to Latest Development in digital economy Best Apps to Earn Money Online in India 1. Meesho Category: Reselling Business Earn by selling products without investment Share products via WhatsApp & social media Profit margin depends on pricing Best For: Housewives, beginners, side income seekers 2. Upwork Category...

Merry Christmas Messages and Statuses | History and Celebration Christmas - Happy Christmas Status and Messages for Social Media

Merry Christmas Messages and Statuses: Wishing you a Merry Christmas 2023! Here are some messages and statuses that you can share with your loved ones: "May your Christmas be filled with joy, laughter, and love. Wishing you a wonderful holiday season!". "Sending you warm wishes and good cheer this Christmas. Have a great time with your family and friends!". "May the spirit of Christmas fill your heart with peace and happiness. Merry Christmas!". "Wishing you a magical and blissful holiday season. Have a Merry Christmas and prosperous New Year!". "May this Christmas bring you all the love and luck in the world. Merry Christmas and a Happy New Year!". Wishing you a Merry Christmas 2023 Here are some Christmas messages that you can use to wish your friends and family a Merry Christmas: 1. Wishing you a Christmas filled with joy, love, and laughter. May the spirit of Christmas bring warmth to your heart and happiness to your home. 2. May y...