Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Arrays in JS

An array is a collection of similar data types under the same name. We may say that they are special variable, which can hold more than one value at a time. Also we may access the values by referring to an index number.

Creating Arrays in JavScript:

Using an array literal: var array_name = [item1, item2, …];

Using JS Keywaord new: var array_name = new Array(“item1”, “item2”, …);

Accessing elements:

We may easily access an array element by referring to the index number.

Example: 
var stud = ["Adarsh", "Harsh", "Shivam"];
console.log(stud[1]);
Here, Harsh will be printed since we are referring to 1st(0-indexing) element of stud array.

Updating elements:

We may easily update an array element again by referring to the index number.

Example: 
var stud = ["Adarsh", "Harsh", "Shivam"];
stud[1] = "Kunj";
console.log(stud[1]);
Here, Kunj will be printed since we are referring to 1st(0-indexing) element of stud array.

Printing the whole elements:

We may easily printall array elements just by calling the array name.

Example: 
var stud = ["Adarsh", "Harsh", "Shivam"];
console.log(stud);
Here, [ 'Adarsh', 'Harsh', 'Shivam' ] will be printed since we are referring to 1st(0-indexing) element of stud array.

Iterating over the array:

Using a for loop and maintaining an index for the elments we may easily do it.

Example: 
var stud = ["Adarsh", "Harsh", "Shivam"];
for(var i = 0 ; i<3;i++){
console.log(stud[i]);
}
Here, we it will print Adarsh, Harsh and Shivam in 3 lines.

Try the following example in the editor below.

You are given array of integers with it’s size. Update and then print array elememts where new_arr[i] = old_arr[i] + i. Please note that you have to print one element in one line.

Sample Input:

4(Size)
1
2
3
4

Sample Output:

1
3
5
7
Start solving Arrays in JS on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test