Jonny
Jonny

Reputation: 1329

How can i place special characters in an array in javascript

In php I can do this to place a back slash inside an array

$symbols = array(".","\\\\");

What is the best method of doing this in javascript with backslash, colon, and semicolon?

Upvotes: 2

Views: 5850

Answers (2)

Unmitigated
Unmitigated

Reputation: 89414

Use an array literal delimited by square brackets:

var symbols = [".","\\", ":", ";"];
console.log(symbols);

See here for a more thorough explanation of arrays and their methods.

Upvotes: 6

sonEtLumiere
sonEtLumiere

Reputation: 4572

you need to scape backslash (backslash is the escape keyword). If you need to push an item to the array use the push() method.

let arr = ['\\', ':'];
arr.push(';');

console.log(arr);   //Outputs ["\", ":", ";"]

Upvotes: 1

Related Questions