Control Statement in Javascript

JavaScript has a similar set of control structures as the C and Java languages. Conditional statements are supported by if…else and switch . Loops are supported by the while , do… while , and for constructs.

What are Control Statements

In any program, a typical statement aims to execute a command or, in simple words, do something. Control Statements are similar to other statements. But they are different at a certain angle. They have the ability to determine which of the other statements should be executed and at what time it should be executed.

Control statements in JavaScript are used to control the flow of a program. They determine the order in which statements are executed based on certain conditions. The main types of control statements in JavaScript are:

  1. Conditional Statements:
    • if statement:
    • javascriptCopy codeif (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false }
    • else if statement:
    • javascriptCopy codeif (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 }
    • switch statement:
    • javascriptCopy codeswitch (expression) { case value1: // code to be executed if expression === value1 break; case value2: // code to be executed if expression === value2 break; // ... more cases default: // code to be executed if none of the cases match }
  2. Loop Statements:
    • for loop:
    • javascriptCopy codefor (initialization; condition; update) { // code to be executed in each iteration }
    • while loop:
    • javascriptCopy codewhile (condition) { // code to be executed as long as the condition is true }
    • do-while loop:
    • javascriptCopy codedo { // code to be executed at least once, then repeated as long as the condition is true } while (condition);
  3. Jump Statements:
    • break statement:
    • javascriptCopy codefor (let i = 0; i < 5; i++) { if (i === 3) { break; // exit the loop when i is 3 } console.log(i); }
    • continue statement:
    • javascriptCopy codefor (let i = 0; i < 5; i++) { if (i === 3) { continue; // skip the rest of the code in this iteration when i is 3 } console.log(i); }
    • return statement:
    • javascriptCopy codefunction exampleFunction() { // code before return if (condition) { return; // exit the function prematurely } // code after return (will not be executed if condition is true) }

These control statements provide the tools necessary to create dynamic and flexible JavaScript programs by altering the flow of execution based on different conditions.

Objects
As we know from the chapter Data types, there are eight data types in JavaScript. Seven of them are called “primitive”, because their values contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store keyed collections of various data and more complex entities. 

In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else. An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything. We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It’s easy to find a file by its name or add/remove a file.

Javascript Training in hyderabad

Contact

Enquiry Now
close slider
Scroll to Top
Call Now Button