Back to articles
JavaScript Arrays 101

JavaScript Arrays 101

via Dev.toHiral

Think of storing multiple values, how will you store it let fruit1 = " Apple " ; let fruit2 = " Banana " ; let fruit3 = " Mango " ; let fruit4 = " Orange " ; let fruit5 = " Grapes " ; This works, but it quickly becomes messy and difficult to manage. So you can use array to store let fruits = [ " Apple " , " Banana " , " Mango " , " Orange " , " Grapes " ]; An array is a collection of values stored in a single variable in a specific order. Now all fruits are stored inside one array called fruits. How to create Array Creating an array is simple. We use square brackets [ ]. let numbers = [10, 20, 30, 40]; let mixed = ["John", 25, true]; Accessing Array Elements (Using Index) Each element in an array has a position number called an index. Important rule: Array indexing starts from 0, not 1. let fruits = [ " Apple " , " Banana " , " Mango " ]; console . log ( fruits [ 0 ]); // Apple console . log ( fruits [ 1 ]); // Banana console . log ( fruits [ 2 ]); // Mango Updating Elements in an Arra

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles