JavaScript Array Manipulation: Adding Elements Dynamically
Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.
an HTML document This JavaScript code defines an array num containing the elements “A”, “B”, and “C”. It then logs the array to the console. After that, it uses the push method to add the element “D” to the end of the array and logs the updated array to the console.with GSAP (GreenSock Animation Platform) and ScrollTrigger for animating text on scroll. This script animates each character in the .quote element with a red background when it comes into view.
<script>
var num = ["A","B","C"];
console.log(num);
num.push("D");
console.log(num);
</script>
var num = ["A","B","C"];
console.log(num);
num.push("D");
console.log(num);
</script>
- Initial array: ["A", "B", "C"]
- Log the initial array to the console.
- Add "D" to the end of the array using num.push("D").
- Updated array: ["A", "B", "C", "D"]
- Log the updated array to the console.
you should see two log statements in the console:
["A", "B", "C"]
New Array Output
["A", "B", "C", "D"]