Javascript Notes

mobiprep (6).png

JavaScript

mobiprep (6).png

Data Types

mobiprep (6).png

Variables

mobiprep (6).png

Operators

mobiprep (6).png

Functions

mobiprep (6).png

Conditional Statements and Loops

mobiprep (6).png

Arrays

mobiprep (6).png

Objects

mobiprep (6).png

DOM Elements

mobiprep (6).png

Cookies

mobiprep (6).png

Pop Up Box

mobiprep (6).png

Exception Handling

Heading

Q

1

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

LRM_EXPORT_207556595493866_20190724_1939

Ans

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
}

LRM_EXPORT_207556595493866_20190724_1939

Q

2

Wrtie a note on looping statements in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

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
}

Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
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);

LRM_EXPORT_207556595493866_20190724_1939

Q

3

Explain break and continue statements in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

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>";
}

LRM_EXPORT_207556595493866_20190724_1939

Q

4

Explain for-in loop in JavaScript.

LRM_EXPORT_207556595493866_20190724_1939

Ans

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
}

LRM_EXPORT_207556595493866_20190724_1939