Explore how to convert a plain text list into a JavaScript array using Visual Studio Code in this informative Emmet tutorial.
This exercise is excerpted from Noble Desktop’s front-end web development (Emmet coding tips) training materials and is compatible with updates through 2022. To continue learning web development with hands-on training, check out our coding bootcamps in NYC and live online.
Wrapping Text on Multiple Lines (Convert a list into JavaScript Array)
We can quickly turn a plain text list like this:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Into a JavaScript Array like this:
days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
Here’s How To Do It
These instructions are for Visual Studio Code:
- Select the multi-line plain text list (such as the days list above).
- Choose Selection > Add Cursors to Line Ends.
- To select the entire line, hold Shift and hit the Home key. If you don’t a Home key, hold Shift then fn (the function key) and hit Left Arrow.
- Type a single-quote (
'
) to wrap them in quotes. - Press Right Arrow twice to move cursor after the quote.
- Type a comma.
- Press Right Arrow to go to next line.
- Hit Delete (Mac) or Backspace (Windows) to pull everything onto one line.
- Delete the extra comma at the end of the line (if there is one).
- Select all the list code that’s now on a single line.
- Type a left square bracket [ to wrap it in brackets.
-
In the front of the bracketed code type days= and you should end up with:
days=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']