All Exams  >   Software Development  >   JavaScript for Web Development  >   All Questions

All questions of Arrays in JS for Software Development Exam

What is the output of the following code?
var arr = [1, 2, 3, 4, 5];
arr.forEach(function(element) {
  console.log(element * 2);
});
  • a)
    2, 4, 6, 8, 10
  • b)
    1, 2, 3, 4, 5
  • c)
    2, 4, 6
  • d)
    1, 4, 9, 16, 25
Correct answer is option 'A'. Can you explain this answer?

Maya Mukherjee answered

Explanation:

forEach method:
- The `forEach` method is used to execute a provided function once for each array element.
- In this code snippet, the `forEach` method is called on the `arr` array.

Function inside forEach:
- The function passed to `forEach` takes an `element` parameter, which represents each element of the array.
- Inside the function, each element is multiplied by 2 using `element * 2`.
- The result of the multiplication is then logged to the console using `console.log`.

Output:
- The output of the code will be the result of multiplying each element of the array by 2.
- So, the output will be: 2, 4, 6, 8, 10.

Therefore, the correct answer is option 'A' - 2, 4, 6, 8, 10.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.join("-"));
  • a)
    1,2,3,4,5
  • b)
    12345
  • c)
    1-2-3-4-5
  • d)
    1-2345
Correct answer is option 'C'. Can you explain this answer?

TeamUnknown answered
The 'join()' method converts all the elements of an array into a string and concatenates them, using the specified separator (in this case, "-").

Which of the following methods can be used to add elements to the end of an array in JavaScript?
  • a)
    'push()'
  • b)
    'pop()'
  • c)
    'shift()'
  • d)
    'unshift()'
Correct answer is option 'A'. Can you explain this answer?

Palak Shah answered


push() method in JavaScript

The push() method in JavaScript is used to add one or more elements to the end of an array. It modifies the original array and returns the new length of the array.

How to use push() method:
- To add a single element:
```
let arr = [1, 2, 3];
arr.push(4);
// arr is now [1, 2, 3, 4]
```

- To add multiple elements:
```
let arr = [1, 2, 3];
arr.push(4, 5);
// arr is now [1, 2, 3, 4, 5]
```

Benefits of using push() method:
- It is a simple and straightforward method to add elements to the end of an array.
- It can be used to add one or multiple elements at once, making it versatile.

Example:
```
let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // Output: [1, 2, 3, 4]
```

Using the push() method is a common practice in JavaScript when you need to append elements to an existing array without altering the existing elements' positions.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.slice(1, 3));
  • a)
    [2, 3]
  • b)
    [1, 2]
  • c)
    [3, 4]
  • d)
    [2, 4]
Correct answer is option 'A'. Can you explain this answer?

KnowIT answered
The 'slice()' method returns a new array containing the elements from index 1 (inclusive) to index 3 (exclusive). In this case, it returns [2, 3].

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.shift());
  • a)
    1
  • b)
    5
  • c)
    4
  • d)
    undefined
Correct answer is option 'A'. Can you explain this answer?

Akash Chawla answered
Code Explanation:
- The given code snippet declares an array named `arr` with the values `[1, 2, 3, 4, 5]`.
- It then uses the `shift()` method on the array `arr` and logs the returned value to the console using `console.log()`.

Output:
The output of the code snippet is `1`.

Explanation:
- The `shift()` method in JavaScript removes the first element from an array and returns that element.
- In this case, the `shift()` method is called on the `arr` array, which contains the values `[1, 2, 3, 4, 5]`.
- So, the first element of the array is `1`.
- The `shift()` method removes `1` from the array and returns it as the result.
- The returned value (`1`) is then logged to the console using `console.log()`.

Therefore, the output of the code snippet is `1`.

What is the output of the following code?
var arr = [1, 2, 3];
console.log(arr.indexOf(3));
  • a)
    -1
  • b)
    0
  • c)
    1
  • d)
    2
Correct answer is option 'D'. Can you explain this answer?

Tanuja Mishra answered
The 'indexOf()' method returns the index of the first occurrence of the specified element in the array. In this case, 3 is at index 2.

What is the output of the following code snippet?
```javascript
let arr = [1, 2, 3, 4, 5];
let index = arr.findIndex((num) => num === 3);
console.log(index);
```
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    -1
Correct answer is option 'B'. Can you explain this answer?

Tom Tattle answered
The 'findIndex()' method returns the index of the first element in the array that satisfies the provided testing function. In this case, it returns the index of the element 3.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.concat([6, 7]));
  • a)
    [1, 2, 3, 4, 5]
  • b)
    [1, 2, 3, 4, 5, 6, 7]
  • c)
    [6, 7]
  • d)
    [2, 3, 4, 5, 6, 7]
Correct answer is option 'B'. Can you explain this answer?

KnowIT answered
The 'concat()' method is used to merge two or more arrays. It returns a new array that contains the elements of the original array followed by the elements of the provided arrays.

What is the output of the following code snippet?
let arr = [1, 2, 3, 4, 5];
console.log(arr.pop());
  • a)
    1
  • b)
    5
  • c)
    4
  • d)
    undefined
Correct answer is option 'B'. Can you explain this answer?

KnowIT answered
The 'pop()' method removes the last element from the array and returns it. In this case, it removes 5 from the array and returns it.

Chapter doubts & questions for Arrays in JS - JavaScript for Web Development 2025 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Arrays in JS - JavaScript for Web Development in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Top Courses Software Development