Append a value in an array through JavaScript

How to add or append values ​​to an array via JavaScript?
Last updated:7/20/2021 1:46:49 AM

1 Answers

Ethan Karla
Ethan Karla

In JavaScript if you want to add a value to an array you can use the function array push. You can understand this method more well if you learn it with the help of an example :

<!DOCTYPE html>

<html>
<body>
    <p id='demo'></p>
    <script>
        const cars = ['BMW', 'Audi', 'Ferrari', 'Bentley'];
        cars.push('Rolls Royce');
        document.getElementById('demo').innerHTML = cars;
    </script>
</body>
</html>

Hope this code will be helpful for you.

Happy Coding!

Answer