➢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 is optimized for learning and giving you Knowledge in all aspects. Concepts might be simplified to improve reading and learning. Education, Coding, and Reference are constantly reviewed to avoid errors, but we cannot warrant the full correctness of all content. While using the bmreducation website, you agree to have read and accepted terms and Conditions , and privacy policy .
© 2023 BMR EDUCATION