Comparison operators
Comparison operators are used to compare two values, returning either true or false.
== loose equality operator → checks whether its two operands are equal after attempting to perform type coercion.
=== strict equality operator → checks for equality without type conversion, both the value and the type must be exactly the same.
In JavaScript, primitive types (like numbers and strings) are compared by value, while objects (including arrays) are compared by reference.
Arrays are a type of object. When an array is created , a reference to that array is created in memory.
Arrays are compared by reference, not by value.
[1, 2, 3] === [1, 2, 3] → returns false
Arrays are used to store multiple values in a single variable.
Each value is called an element, and each element has a numeric position in the array, known as its index.
Arrays are zero-indexed, meaning the first element is at index 0, the second at index 1, and so on.
Arrays can contain any data type, including numbers, strings, and objects.
const arr1 = [2, 4, 6]; array
arr1[0]; element at index 0 → 2
arr1[1]; element at index 1 → 4
arr1[2]; element at index 2 → 6
arr1[3]; element at index 3 → undefined index not found
Strings are a sequence of zero or more characters written inside quotes used to represent text.
Strings may consist of letters, numbers, symbols, words, or sentences.
Strings are immutable, they cannot be changed.
Each character in a string has an index.
The first character will be index 0 the second character will be index 1 and so on.
There are two ways to access an individual character in a string.
charAt() method
const str1 = "abc"; string
str1.charAt(0); character at index 0 → "a"
str1.charAt(1); character at index 1 → "b"
str1.charAt(2); character at index 2 → "c"
str1.charAt(3); character at index 3 → "" index not found
Alternatively use at() or slice() methods
bracket notation []
const str2 = "abc"; string
str2[0]; character at index 0 → "a"
str2[1]; character at index 1 → "b"
str2[2]; character at index 2 → "c"
str2[3]; character at index 3 → undefined index not found
Numbers are used to represent both integer and floating-point values.
Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴
let num1 = 5; → number
let num2 = 2.5; → number
let num3 = num1 + num2;
console.log(num3); returns ↴
7.5 → number
Compare two arrays for equality using ↴
every() method → method returns false if it finds one element in the array that does not satisfy the provided testing function. Otherwise, it returns true.
length property → sets or returns the number of elements in an array.
every() method returns false if it finds one element in the array that does not satisfy the provided testing function. Otherwise, it returns true.
Check if every number in the array is an even number.
const arr2 = [2, 4, 6, 8, 10];
arr2.every((x) => x % 2 === 0); returns ↴
true → every element in the array is divisible by 2
const arr3 = [2, 4, 6, 8, 9];
arr3.every((x) => x % 2 === 0); returns ↴
false → every element in the array is not divisible by 2
length property returns the number of elements in an array.
const arr4 = [1, 2, 3, 4, 5, 6];
arr4.length; returns ↴
6 → there are 6 elements in the array
Initialize variables to hold the two arrays to compare for equality.
const array1 = ["Hello", "World", 2, 3]; → user input
const array2 = ["Hello", "World", 2, 3]; → user input
Define a function arraysEqual to compare two arrays for equality.
function arraysEqual(arr) {}
The function takes two arrays as input arr1, arr2 and returns true if both arrays are equal, otherwise returns false. The original arrays are unchanged.
Check if the lengths of the arrays are different.
if (arr1.length !== arr2.length)
If lengths differ, arrays are not equal. Return false and end execution of function.
every() method executes a function for each array element.
every(callbackFn)
callback function ↴
(value, index) => value === arr2[index]
Return true if every element in arr1 matches the corresponding element in arr2
return arr1.every((value, index) => value === arr2[index])
Return false if one or more elements does not match the corresponding element.
If the function returns true, the arrays are equal ✔
If the function returns false, the arrays are not equal ✖
Call the function with ↴
arraysEqual(array1, array2);
Compare two arrays for equality.
const array1 = ["Hello", "World", 2, 3];
const array2 = ["Hello", "World", 2, 3];
function arraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
return arr1.every((value, index) => value === arr2[index]);
}
call function
arraysEqual(array1, array2); returns ↴
true