Software Development Exam  >  Software Development Questions  >  What will be the output of the following code... Start Learning for Free
What will be the output of the following code?
var arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
  • a)
    [0, 1, 2, 3]
  • b)
    [1, 2, 3]
  • c)
    [0, 2, 3]
  • d)
    [0, 1, 3]
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
What will be the output of the following code?var arr = [1, 2, 3];arr....
The 'unshift()' method adds an element to the beginning of the array, shifting all other elements to higher indexes.
View all questions of this test
Most Upvoted Answer
What will be the output of the following code?var arr = [1, 2, 3];arr....
Understanding the Code
The code snippet provided is as follows:
javascript
var arr = [1, 2, 3];
arr.unshift(0);
console.log(arr);
This code demonstrates the use of the `unshift` method in JavaScript.
What Does `unshift` Do?
- The `unshift` method adds one or more elements to the beginning of an array.
- It modifies the original array and returns the new length of the array.
Step-by-Step Execution
1. Initial Array: The variable `arr` is initialized with the array `[1, 2, 3]`.
2. Using `unshift`: The `unshift(0)` method is called on `arr`:
- This adds the number `0` to the front of the array.
- The array now looks like `[0, 1, 2, 3]`.
3. Output: The `console.log(arr)` statement prints the modified array to the console.
Conclusion
- After executing the `unshift` method, the array `arr` is transformed from `[1, 2, 3]` to `[0, 1, 2, 3]`.
- Therefore, the correct answer is option A: `[0, 1, 2, 3]`.
This succinctly illustrates how `unshift` works, confirming that the output reflects the addition of `0` at the start of the array.
Explore Courses for Software Development exam

Top Courses for Software Development

Question Description
What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer?.
Solutions for What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer?, a detailed solution for What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will be the output of the following code?var arr = [1, 2, 3];arr.unshift(0);console.log(arr);a)[0, 1, 2, 3]b)[1, 2, 3]c)[0, 2, 3]d)[0, 1, 3]Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev