Arrays in JS
Arrays in JS
This works fine, but managing so many variables becomes hard when you have more data.
Instead, we can use arrays.
An array is a variable-like structure that can hold multiple values in a single place.
The Array.from() static method creates a new, The Array.of() static method creates a new
shallow-copied Array instance from an iterable Array instance from a variable number of
or array-like object. arguments, regardless of number or type of
the arguments.
Questions
What is the output of the following?
a) Array.of(1);
b) Array.of(1,2,3);
c) Array.of(undefined);
Types of Array
1D Array 2D Array (Matrix) 3D Array (Multidimensional Array)
Syntax
exampl
e
Methods Description
join()
concat()
flat()
used to flatten an array, meaning it removes nested arrays and returns a new
array with all elements at a single depth level.
Loops
1 for loop = Known number of iterations + Iterate over an array
.
4 for…of = Execute at least once, then check the condition + Iterating over an array of
. numbers.
The for...of loop iterates over iterable objects like arrays, strings, maps, and sets.
5 for…in = Iterating over keys in objects or indices in arrays + Iterating over object
. properties
Questions
Q1. Linear Search
Given Array = [90,100,30,40,20]
Find index at which element 20 is present?
b) Compare with ==
alert( 0 == '' ); alert( 0 == [] ); alert( [] == [] );
c) == v/s ===
console.log( null == undefined); console.log( null === undefined);