JavaScript Array add Items at the beginning

Description

JavaScript Array add Items at the beginning

let languages = ["CSS", "HTML", "Java"];

// Prepend a single element to languages array
languages.unshift("C++");

console.log(languages);/*from  w w  w  .jav a  2s.c  o m*/
// Prints: ["C++", "CSS", "HTML", "Java"]

// Prepend multiple elements to languages array
languages.unshift("Python", "Javascript");

console.log(languages);
// Prints: ["Python", "Javascript", "C++", "CSS", "HTML", "Java"]

// Loop through languages array and display all the values
for(let i = 0; i < languages.length; i++){
    console.log("<p>" + languages[i] + "</p>");
}



PreviousNext

Related