0% found this document useful (0 votes)
2 views10 pages

Arrays in JS

The document introduces arrays as a solution for managing multiple values efficiently. It covers various methods for creating arrays, accessing elements, and common operations such as slicing and concatenation. Additionally, it discusses different types of arrays, looping techniques, and provides practice questions related to searching and comparisons.

Uploaded by

paidcourse.lata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Arrays in JS

The document introduces arrays as a solution for managing multiple values efficiently. It covers various methods for creating arrays, accessing elements, and common operations such as slicing and concatenation. Additionally, it discusses different types of arrays, looping techniques, and provides practice questions related to searching and comparisons.

Uploaded by

paidcourse.lata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Introduction to Arrays

Imagine you have marks for five subjects:

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.

Accessing Array Elements


Different ways to create an array
1. Array Literal Notation (Most Common) 2. Using Array Constructor (new)

3. Using Array.from() 4. using Array.of()

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)

console.log(arr[2]); console.log(matrix[1][2]); console.log(cube[1][0][1]);


Methods used in Array operations
Commonly Used Methods
Feature slice() splice()
Purpose Extract elements Add, remove, replace elements
Diagram

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
.

2 while loop = Unknown iterations; depends on a condition


. The while loop executes as long as the specified condition is true.
3 do…while = Execute at least once, then check the condition.
.

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?

Q2. Binary Search


Given Array = [10,20,30,40,50]
Find 20 is present or not in the array?

Q3. Find largest element in an array.


Input: arr[] = [11,20,30,40,50]
Output: 50
Practice Questions
What is the output of the following?
a) Concentration
Arrays Strings
alert( [] + 1 ); alert( "" + 1 );
alert( [1] + 1 ); alert( "1" + 1 );
alert( [1,2] + 1 ); alert( "1,2" + 1 );

b) Compare with ==
alert( 0 == '' ); alert( 0 == [] ); alert( [] == [] );

alert('0' == '' ); alert('0' == [] ); alert( [0] == [0] );

c) == v/s ===
console.log( null == undefined); console.log( null === undefined);

You might also like