Last active
May 22, 2021 02:34
-
-
Save futur/5755392 to your computer and use it in GitHub Desktop.
Understanding identifiers (valid and invalid) in JS. Simple examples.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var $ = {'4s':'4 times S', s4:'s times 4'}; // correct identifiers and valid. | |
| // var _ = {4s:'4 times S', s4:'s times 4'}; // Error in 4s, as its not a valid identifier. coz identifiers cant start with numbers. so add quotes. | |
| console.log($['4s']); // 4 times S - this works because 4s is basically invalid identifier and thus we enclosed with quotes to make it work | |
| // console.log($.4s); //Error since the 4s is not a valid identifier anc . operator will ignore with error. | |
| console.log($.s4); //s times 4 | |
| var object ={.12e34 : 'hey'} | |
| console.log(object['.12e34']); //undefined | |
| console.log(object[.12e34]); //hey | |
| console.log(JSON.stringify(object)); //{"1.2e+33":"hey"} | |
| //So internally .12e34 is stored as "1.2e+33", to prove this: | |
| console.log(String(.12e34)); //1.2e+33 | |
| var myName = {பிரேம்:'குமார் '}; | |
| console.log(myName.பிரேம்); | |
| console.log(JSON.stringify(myName)); | |
| //Ultimately, we understand that every identifier is checked for validity by the runtime internally then converted to a valid String. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, thanks for creating this! Now I have a better understanding of identifiers. For some reason I was confused until I read your docs. Thanks!