While loop executes a set of statements as long as a condition is true. It requires relevant variables to be ready such as an indexing variable to iterate through a loop.
The for loop is C-style in that it requires three parts, the initialization or loop variant, the condition and advancement to next iteration. It is used to iterate over data when referring to a changing index.
For Loop
Sample Codelet even_numbers = [2,4,6,8,10];
for (let i = 0; i < even_numbers.length; i++) {
let even_number = even_numbers[i];
console.log(even_number . “\n”);
}
let names = [“Jake”, “John”, “James”];
for (let n = 0; n < names.length; n++) {
let name = names[i];
console.log(name . “\n”);
}
Loops execute one or more statements up to a specific number of times. Loops allow repetitive tasks to be executed using minimal code. Loops allow complex tasks to be simplified by automating similar operations.
Table : Loops
Type
Description
Syntax
for
loops through code block a specific number of times
for (expr1; expr2; expr3) statement
for in
loops over Array properties
for (variable in array) statement
for of
loops through Object values
for (variable of object) statement
while
loops through code block until specific condition is false
JavaScript supports logical conditionals for mathematics in the form of comparison operators. Logical conditions are used to perform different actions based of different decisions.
JavaScript has the following conditional statements, if, else if, else and switch.
Use if to specify a block of code to be executed, if a specified condition is true.
If
Sample Codelet num1 = 10;
let num2 = 5;
if (num1 > num2) {
console.log(“num1 is greater than num2”);
}
Use else if to specify a new condition to test, if the first condition is false.
Else if
Sample Codelet num3 = 11;
let num4 = 11;
if (num3 > num4) {
console.log(“num3 is greater than num4”);
}
else if (num3 == num4) {
console.log(“num3 and num4 are equal”);
}
Use else to specify a block of code to be executed, if the same condition is false.
Else
Sample Codelet num5 = 22;
let num6 = 33;
if (num5 > num6) {
console.log(“num5 is greater than num6”);
else if (num5 == num6) {
console.log(“num5 and num6 are equal”);
}
else {
console.log(“num5 is not greater than num6”);
}
Use switch to specify many alternative blocks of code to be executed. JavaScript uses switch case pair common in other programming languages. The break keyword stops the execution inside the switch block.
Switch
Sample Codelet num7 = 41;
let num8 = 77;
switch (true) {
case (num7 > num8):
console.log(“num7 is greater than num8”);
break;
case (num7 == num8):
console.log(“num7 and num8 are equal”);
break;
default:
console.log(“num8 is not greater than num7”);
}
An array specifies a variable that can be indexed as a list in rows and columns. The first index is zero as is common in most programming languages.
Arrays
Sample Codelet even_numbers = [2,4,6,8,10];
console.log(even_numbers);
console.log(even_numbers.length); // Number Of Items In List
console.log(even_numbers[1]); // Access Array Item By Index Number
even_numbers.pop(); // Remove Last Item
console.log(even_numbers);
even_numbers.push(10); // Add New Item At The End
console.log(even_numbers);
even_numbers.shift(); // Remove First Item
console.log(even_numbers);
even_numbers.unshift(2); // Add New Item At The Beginning
console.log(even_numbers);
let even_numbers2 = [12, 14, 16];
let combined = even_numbers.concat(even_numbers2); // Merge 2 Arrays
console.log(combined);
Associate array stores a collection of named key and value pairs.
JavaScript logical operators are used to combine conditional statements. Each operand is a conditional that can be evaluated to a true or false value. The value of the conditions determines the overall value of the operator or grouping.
Logical operators are use to control flow of the application. They are normally found as part of a control statement such as if and while.
Table : Logical Operators
Logic
Name
Example
&&
And
num1 && num2
||
Or
num1 || num2
!
Not
!num1
Logical Operators
Sample Codelet num1 = 5;
let num2 = 10;
if ( num1 > 0 && num2 > 0 ) {
console.log(“Both AND conditions are true”);
} else {
console.log(“This condition grouping is false”);
}
if ( num1 > 0 || num2 > 0 ) {
console.log(“At least one OR condition is true”);
} else {
console.log(“Both conditions are false”);
}
JavaScript increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. JavaScript supports pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.
Table : Increment And Decrement Operators
Increment / Decrement
Name
Example
++variable
Pre-increment
++num1;
variable++
Post-increment
num1++;
–variable
Pre-decrement
–num1;
variable–
Post-decrement
num1–;
+=
Increment
num+=1;
-=
Decrement
num-=1;
Increment And Decrement Operators
Sample Codelet num = 1;
num += 1; // Increment By One
console.log(num1); // Console Displays 2
num -= 1; // Decrement By One
console.log(num1); // Console Displays 1
num++; // Post-increment
console.log(num1); // Console Displays 2
num–; // Post-decrement
console.log(num1); // Console Displays 1
Assignment operators are used to assign a value to a variable or an expression. The assignment types can be arithmetic, logical, bitwise, shift, and compound operators.