Adding item to the beginning of an array using JavaScript

Leave a Comment

If you want to add an item to the beginning of an array, use the unshift() command.

Here’s an example of adding the bold value to the beginning of the properties array:
var properties = ['orange', '14px', 'Arial'];
properties.unshift('bold');

After this code runs, the array properties contains four elements: [‘bold’, ‘orange’, ‘14px’, ‘Arial’]. As with push(), you can use unshift() to insert multiple items at the beginning of an array:
properties.unshift('bold', 'italic', 'underlined');

0 comments:

Post a Comment