JavaScript Class Notes: Conditional Statements and Loops

Updated: Jun 2

Mobiprep has created last-minute notes for all topics of JavaScript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on JavaScript.

  1. JavaScript: Introduction

  2. Data Types

  3. Variables

  4. Operators

  5. Functions

  6. Conditional Statements and Loops

  7. Arrays

  8. Objects

  9. DOM Elements

  10. Cookies

  11. Pop Up Box

  12. Exception Handling

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. Write a note on if…else statement in JavaScript along with it's syntax.

  2. Write a note on looping statements in JavaScript.

  3. Explain break and continue statements in JavaScript.

  4. Explain for-in loop in JavaScript.

JavaScript Notes: Conditional Statements and Loops Lecture Notes

Question 1. Write a note on if…else statement in JavaScript along with it's syntax.

The 'if...else' statement is the next form of control statement that allows JavaScript to execute statements in a more controlled way.

Syntax:
if (expression) {
Statement(s) to be executed if expression is true
} else {
Statement(s) to be executed if expression is false
}
 

Question 2. Write a note on looping statements in JavaScript.

Loops are handy, if you want to run the same code over and over again, each time with a different value.

Different Kinds of Loops

JavaScript supports different kinds of loops:

  • for - loops through a block of code a number of times

  • for/in - loops through the properties of an object

  • for/of - loops through the values of an iterable object

  • while - loops through a block of code while a specified condition is true

  • do/while - also loops through a block of code while a specified condition is true

Syntax for loop:

for (statement 1; statement 2; statement 3) {
// code block to be executed
}
  1. Statement 1 : is executed (one time) before the execution of the code block.

  2. Statement 2 : defines the condition for executing the code block.

  3. Statement 3 : is executed (every time) after the code block has been executed


The For In Loop

The JavaScript for in statement loops through the properties of an Object:

Syntax:


for (key in object) {
// code block to be executed
}

The For Of Loop

The JavaScript for of statement loops through the values of an iterable object.

It lets you loop over iterable data structures such as Arrays, Strings, Maps, NodeLists, and more:

Syntax

for (variable of iterable) {
// code block to be executed
}

variable - For every iteration the value of the next property is assigned to the variable. Variable can be declared with const, let, or var.

iterable - An object that has iterable properties.


The While Loop

The while loop loops through a block of code as long as a specified condition is true.

Syntax


while (condition) {
// code block to be executed
}

The Do While Loop

The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax:

do {
// code block to be executed
}
while (condition);


 

Question 3. Explain break and continue statements in JavaScript.

The break statement "jumps out" of a loop.

The continue statement "jumps over" one iteration in the loop.


The Break Statement:

It was used to "jump out" of a switch() statement.

The break statement can also be used to jump out of a loop:

Example:

for (let i = 0; i < 10; i++) {
if (i === 3) { break; }
text += "The number is " + i + "<br>";
}

In the example above, the break statement ends the loop ("breaks" the loop) when the loop counter (i) is 3.


The Continue Statement

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop.

This example skips the value of 3:

Example:

for (let i = 0; i < 10; i++) {
if (i === 3) { continue; }
text += "The number is " + i + "<br>";
}
 

Question 4. Explain for-in loop in JavaScript.

The For In Loop The JavaScript for in statement loops through the properties of an Object:

Syntax:

for (key in object)
{
// code block to be executed


Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB


 

Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.

Back to Top

 


11 views0 comments

Recent Posts

See All