➢Converting Arrays to Strings
➢The JavaScript method toString() converts an array to a string of (comma separated) array values.
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
document.getElementById("demo" ).innerHTML = cars.toString();
➢The join() method also joins all array elements into a string.
➢It behaves just like toString(), but in addition you can specify the separator:
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
document.getElementById("demo" ).innerHTML = cars.join(" * " );
RESULT:
➢When you work with arrays, it is easy to remove elements and add new elements.
➢This is what popping and pushing is:
➢Popping items out of an array, or pushing items into an array.
➢The pop() method removes the last element from an array:
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
cars.pop();
➢The pop() method returns the value that was "popped out":
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
let car = cars.pop();
➢The push() method adds a new element to an array (at the end):
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
cars.push("Volvo" );
➢The push() method returns the new array length:
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
let car = cars.push("Volvo" );
➢The shift() method removes the first array element and "shifts" all other elements to a lower index.
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
cars.shift();
➢The shift() method returns the value that was "shifted out":
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
let car = cars.shift();
➢The unshift() method adds a new element to an array (at the beginning), and "unshifts" older elements:
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
cars.unshift("Volvo" );
➢The unshift() method returns the new array length.
EXAMPLE:
const cars = ["Saab" , "Volvo" , "BMW" ];
cars.unshift("Volvo" );
BMR EDUCATION
BMR EDUCATION is provided for learning and imparting knowledge in all aspects. Concepts may be simplified to enhance readability and learning. The content we provide is regularly reviewed to minimize errors, but we cannot guarantee the absolute correctness of all content. When using the BMR Education website, you agree to have read and accepted the Terms and Conditions , and Privacy Policy .
BMR EDUCATION © 2023 All rights reserved