I want to store emojis in an array and print it using console.out(); on the browser's console ?
Asked
Active
Viewed 1,984 times
-1
-
`var a = ['']; console.info(a[0]);` – Thilo Feb 18 '17 at 13:42
-
2Possible duplicate of [How can I split a string containing emoji into an array?](http://stackoverflow.com/questions/24531751/how-can-i-split-a-string-containing-emoji-into-an-array) – Rahul Feb 18 '17 at 13:44
-
@Thilo. I have saved the above code in a js file and then running it, on the console i am getting this garbage value --- 🙀. please explain. – manish Feb 18 '17 at 14:03
-
Is that JS file properly recognized as UTF-8? You can paste the code into the console in your browser and it works (at least for me). – Thilo Feb 18 '17 at 14:27
1 Answers
4
They're just characters so you can use Unicode escapes. Just find the one you want and use \u plus the codepoint value: \u{xxxxx} where xxxxx is the codepoint. The syntax with the curly braces only works in ES6. You can only use four hex digits with the original \uxxxx format so if you're stuck pre-ES6 you have to use "surrogate pairs," which is explained in the linked article.
var a = [ '\u{1F984}' ];
console.log(a[0]);
Running this in Node:
$ node
> var a = [ '\u{1F984}' ];
undefined
> console.log(a[0]);
undefined
You should see a Unicorn--a white horse with a horn.
If you don't see a Unicorn then maybe you're using a terminal that doesn't support Unicode. I'm using the macOS terminal. Try it in your browser JavaScript console.

Willis Blackburn
- 8,068
- 19
- 36
-
Sir.I have saved the above code in a js file and then running it, on the console i am getting this garbage value " " not any emojis. – manish Feb 18 '17 at 14:08
-
1You should see the Unicorn emoji. If you want a different emoji, change 1F984 to something else. See the linked page for the complete list. – Willis Blackburn Feb 18 '17 at 14:11