🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

While Loop

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.

While Loop

Sample Codelet counter = 0;
while (counter < 10) {
	console.log(counter);
	counter++;
}
// Type Some Code

For 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”);
}
// Type Some Code

Chapter 6: Loops

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

TypeDescriptionSyntax
forloops through code block a specific number of timesfor (expr1; expr2; expr3) statement
for inloops over Array propertiesfor (variable in array) statement
for ofloops through Object valuesfor (variable of object) statement
whileloops through code block until specific condition is falsewhile (expr) statement
do whileloop once before checking condition is truedo statement while (expr)

Table : Common Functions Used With Loops

FunctionDescriptionExample
breakEnd execution of current structurebreak
continueSkip rest of current loop iterationcontinue

Without Loops

Sample Codelet even_numbers = [2,4,6,8,10];
console.log(even_numbers[0]);
console.log(even_numbers[1]);
console.log(even_numbers[2]);
console.log(even_numbers[3]);
console.log(even_numbers[4]);
let names = [“Jake”, “John”, “James”];
console.log(names[0]);
console.log(names[1]);
console.log(names[2]);
// Type Some Code

Chapter 5: If Else Conditional Statement

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”);
}
// Type Some Code

Chapter 4: Arrays

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.

Associative Arrays

Sample Codelet pickup_truck = [“brand”=>”Ford”, “model”=>”F-Series”, “year”=>1984];
console.log(pickup_truck[“model”];

A two-dimensional array is an array of arrays requiring two indices to select an element.

Two-dimensional Arrays

Sample Codelet int_array = [ [11,22,33], [111,222,333]];
let array2D =  [[“Ford”,”F-Series”,1984],[“GMC”,”C-Series”,1986]];
console.log( array);
console.log( array2D);

Multidimensional arrays are two or more levels deep containing one or more arrays.

Multidimensional Arrays

Sample Codelet students = [
    “john_doe” => [
        “name” => “John”,
        “surname” => “Doe”,
        “age” => 25,
    ],
    “jane_doe” => [
        “name” => “Jane”,
        “surname” => “Doe”,
        “age” => 25,
    ]
];
console.log(students);
console.log(students[‘john_doe’][‘surname’]);

Table : Common Functions For Manipulating Lists

FunctionDescriptionExample
Array.lengthDetermine how many itemseven_numbers.length;
Array.push()Append itemseven_numbers.push(10);
Array.unshift()Insert item at the beginningeven_numbers.unshift(2);
Array.shift()Remove item at the beginningeven_numbers.shift();
Array.pop()Pop the item at the endeven_numbers.pop();
Array.concatMerge arrayseven_numbers.concat(even_numbers2);
Array.indexOf()Returns position of item
even_numbers.indexOf(8);
Array.includes()Check if item is presenteven_numbers.includes(4);
Array.sortSort in alphabetic ordereven.numbers.sort();
Array.reverse()Sort in reverse ordereven_numbers.reverse();
// Type Some Code

Logical Operators

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

LogicNameExample
&&Andnum1 && num2
||Ornum1 || 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”);
}
// Type Some Code

Increment And Decrement Operators

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 / DecrementNameExample
++variablePre-increment++num1;
variable++Post-incrementnum1++;
–variablePre-decrement–num1;
variable–Post-decrementnum1–;
+=Incrementnum+=1;
-=Decrementnum-=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
// Type Some Code

Comparison Operators

Comparison operators compare two values which can be numbers or strings.

Table : Comparison Operators

ComparisionNameExample
==Equalnum1 == num2;
!=Not Equalnum1 != num2;
>Greater Thannum1 > num2;
<Less Thannum1 < num2;
>=Greater Than Or Equal Tonum1 >= num2;
<=Less Than Or Equal Tonum1 <= num2;

Comparison Operators

Sample Codelet name1 = “John”;
let name2 = “James”;
console.log(name1 == name2); // Console Displays False
console.log(name1 == 10); // Console Displays False
let num1 = 12;
let num2 = 22;
console.log(num1 > num2); // Console Displays False
console.log(num1 == 10); // Console Displays False
// Type Some Code

Assignment Operators

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.

Table : Assignment Operators

AssignmentEquivalentDescription
num1 = num2num1 = num2Set left operand to same value on right
num1 += num2num1 = num1 + num2Addition
num1 -= num2num1 = num1 – num2Subtraction
num1 *= num2num1 = num1 * num2Multiplication
num1 /= num2num1 = num1 / num2Division
num1 %= num2num1 = num1 % num2Modulus
num1 **= num2num1 = num1 ** num2Exponentiation
num1 &= num2num1 = num1 & num2Bitwise AND
num1 |= num2num1 = num1 | num2Bitwise OR
num1 ^= num2num1 = num1 ^ num2Bitwise XOR
num1 >>= num2num1 = num1 >> num2Bitwise Right Shift, Assign Left
num1 <<= num2num1 = num1 << num2Bitwise Left Shift, Assign Right

Assignment Operators

Sample Codelet num1 = 5;
let num2 = 3;
let num_add = num1;
num_add += num2;
let num_sub = num1;
num_sub -= num2;
let num_mul = num1;
num_mul *= num2;
let num_div = num1;
num_div /= num2;
let num_mod = num1;
num_mod %= num2;
let num_exp = num1;
num_exp **= num2;
console.log(num_add); // Console Displays 8
console.log(num_sub); // Console Displays 2
console.log(num_mul); // Console Displays 15
console.log(num_div); // Console Displays 1.66
console.log(num_mod); // Console Displays 2
console.log(num_exp); // Console Displays 125
// Type Some Code

Arithmetic Operators

The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, floor division and modulus operations.

Table : Arithmetic Operators

OperatorNameExample
+Addition Or Sumnum1 + num2
Subtraction Or Differencenum1 – num2
*Multiplication Or Productnum1 * num2
/Division Or Quotientnum1 / num2
%Modulus Or Division Remaindernum1 % num2
**Exponentiation Or Raising Powernum1 ** num2
Math.floor()Floor DivisionMath.floor(num1 / num2)


Arithmetic Operators

Sample Codelet num1 = 5;
let num2 = 3;
let num_add = num1 + num2;
let num_sub = num1 – num2;
let num_mul = num1 * num2;
let num_div = num1 / num2;
let num_mod = num1 % num2;
let num_exp = num1 ** num2;
let num_flo = Math.floor(num1 / num);
console.log(num_add); // Console Displays 8
console.log(num_sub); // Console Displays 2
console.log(num_mul); // Console Displays 15
console.log(num_div); // Console Displays 1.66
console.log(num_mod); // Console Displays 2
console.log(num_exp); // Console Displays 125
console.log(num_flo); // Console Displays 1
// Type Some Code