JavaScript Class Notes: Arrays

Updated: Jun 2

Mobiprep has created last-minute notes for all topics of Javascript to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on javascript.

  1. JavaScript: Introduction

  2. Data Types

  3. Variables

  4. Operators

  5. Functions

  6. Conditional Statements and Loops

  7. Arrays

  8. Objects

  9. DOM Elements

  10. Cookies

  11. Pop Up Box

  12. Exception Handling

JavaScript Class Notes: Arrays

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. Explain Array Object in JavaScript.

  2. What are the uses of arrays in JavaScript?

  3. What are the operations supported by Arrays in JavaScript?

  4. Explain the pop() and push() methods in JavaScript.

  5. Explain the use of shift() and unshift() methods.


JavaScript: Arrays


Question 1. Explain Array Object in JavaScript.

The Array object lets you store multiple values in a single variable. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Syntax


var fruits = new Array( "apple", "orange", "mango" );


Question 2. What are the uses of arrays in JavaScript? 1. Arrays allow random access to elements. This makes accessing elements by position faster. 2. Arrays have better cache locality that makes a pretty big difference in performance. 3. Arrays represent multiple data items of the same type using a single name.


Question 3. What are the operations supported by Arrays in JavaScript?

  • concat() : Joins two or more arrays, and returns a copy of the joined arrays

  • copyWithin(): Copies array elements within the array, to and from specified positions

  • entries(): Returns a key/value pair Array Iteration Object

  • every(): Checks if every element in an array pass a test

  • fill(): Fill the elements in an array with a static value

  • filter(): Creates a new array with every element in an array that pass a test

  • find(): Returns the value of the first element in an array that pass a test

  • findIndex(): Returns the index of the first element in an array that pass a test

  • forEach(): Calls a function for each array element

  • from(): Creates an array from an object

  • includes(): Check if an array contains the specified element

  • indexOf(): Search the array for an element and returns its position

  • isArray(): Checks whether an object is an array

  • join(): Joins all elements of an array into a string

  • keys(): Returns a Array Iteration Object, containing the keys of the original array

  • lastIndexOf(): Search the array for an element, starting at the end, and returns its position

  • map(): Creates a new array with the result of calling a function for each array element

  • pop(): Removes the last element of an array, and returns that element

  • push(): Adds new elements to the end of an array, and returns the new length

  • reduce(): Reduce the values of an array to a single value (going left-to-right)

  • reduceRight(): Reduce the values of an array to a single value (going right-to-left)

  • reverse(): Reverses the order of the elements in an array

  • shift(): Removes the first element of an array, and returns that element

  • slice(): Selects a part of an array, and returns the new array

  • some(): Checks if any of the elements in an array pass a test

  • sort(): Sorts the elements of an array

  • splice(): Adds/Removes elements from an array

  • toString(): Converts an array to a string, and returns the result

  • unshift(): Adds new elements to the beginning of an array, and returns the new length

  • valueOf(): Returns the primitive value of an array:

Question 4. Explain the pop() and push() methods in JavaScript.

push() method The push() method allows you to add one or more elements to the end of the array. The push() method returns the value of the length property that specifies the number of elements in the array. If you consider an array as a stack, the push() method adds one or more element at the top of the stack. The following example creates an empty array named stack and adds five numbers, one by one, at the end of the stack array. It is like to push each number into the top of the stack.


let stack = [];
stack.push(1);
console.log(stack); // [1]

stack.push(2);
console.log(stack); // [1,2]

stack.push(3);
console.log(stack); // [1,2,3]

stack.push(4);
console.log(stack); // [1,2,3,4]

stack.push(5);
console.log(stack); // [1,2,3,4,5]

pop() method

The pop() method removes the element at the end of the array and returns the element to the caller. If the array is empty, the pop() method returns undefined. The following example shows how to pop elements from the top of the stack using the pop() method.

console.log(stack.pop()); // 5
console.log(stack); // [1,2,3,4];

console.log(stack.pop()); // 4
console.log(stack); // [1,2,3];

console.log(stack.pop()); // 3
console.log(stack); // [1,2];

console.log(stack.pop()); // 2
console.log(stack); // [1];

console.log(stack.pop()); // 1
console.log(stack); // []; -> empty

console.log(stack.pop()); // undefined
Code language: JavaScript (javascript)



Question 5. Explain the use of shift() and unshift() methods.

shift() is a built-in JavaScript function that removes the first element from an array. The shift() function directly modifies the JavaScript array with which you are working. shift() returns the item you have removed from the array. The shift() function removes the item at index position 0 and shifts the values at future index numbers down by one. If you are struggling to remember the purpose of the shift() function, tell yourself this: shift() shifts the index values in an array down by one. shift() returns undefined if you use the shift() function on an empty list. Syntax:

array_name.shift()

The JavaScript array shift() function takes in no parameters. This is because shift() only performs one function. shift() removes an item from the end of an array. If shift() let you remove an item from any position, a parameter would be needed. unshift() function adds one or more elements to the start of an array. The unshift() function changes the array and returns the length of the new array. Syntax:

array_name.unshift(item_one, item_two,)


The unshift() function takes in as many parameters as you specify. These parameters will all be added to the start of the array.




Top 40 Javascript Interview Questions and Answers
.pdf
Download PDF • 1.15MB


 

Mobiprep is a one stop solution for all exam preparation needs. Get all exam prep resources at a single platform.

Back to Top

 


11 views0 comments

Recent Posts

See All