Today I want to share a few important rules about writing in JavaScript that I feel is important for you to know, especially if you are just starting your coding journey or overall seeking to better your understanding of the language. As a self-taught developer learning JavaScript, I don't recall finding JavaScript tutorials catered to beginners that explicitly taught the lexical structure of the language so I figured I'd share with you.
Feel free to bookmark this short post to keep as a basic reference as it will be helpful to be mindful of these rules as you start building stuff with JavaScript!
What Does Lexical Structure Mean?
It's not as complex as it sounds. Every programming language has a lexical structure, which is a set of basic rules you must follow when you're writing code in a language such as how to write comments and name variables.
JavaScript Is Written In Unicode
Unicode is an international encoding standard supported by almost every writing system and is used in JavaScript, which means you can use emojis or Japanese characters in your code for instance. If you want to read more about unicode (it's not mandatory learning in order to write JavaScript code), go here.
Whitespace
Whitespace does not affect your code in JavaScript. You can hit indent and tab as much as you want but if I'm working in an already existing codebase, I usually follow their formal or style of indenting. If you're more experienced with coding, you may be aware of tools like Prettier which help format your code to be readable.
Semicolons Are Optional
Semicolons are generally used to indicate the end of a statement but they are not mandatory. You will not encounter an error if you were to choose to not end a variable you declare with a semicolon. I personally use semicolons because to me it makes code more readable for my eye but you don't have to use them.
JavaScript is Case-Sensitive
The spelling of the names of your variables, functions, keywords and other identifiers must be exact. So that means myName and MyName are not the same. Another example is with booleans, a topic we'll be touching on later in the series. If we were to set a variable to True instead of true, you would encounter an error.
Comments
There are 2 different ways to create comments. You can either make a single line comment using 2 forward slashes or create a multi-line comment using a forward slash and an asterisk to create longer, more meaningful comments if you need to.
What Are Identifiers?
Identifiers are names. It's how we create references to our variables and functions. You can start any variable name with a dollar sign, underscore or letter. The main rule is to not use numbers as the first character in our identifiers.
Reserved Words
Many of these reserved words are already used in JavaScript like let for instance. It would be rather confusing if we decided to create a declare a let variable and name it let.
Literals
A literal is a piece of data that appears directly in a program such as numbers, strings (text encapsulated by quotation marks), boolean values and null.
So...What are we learning next?
So next we will continue on diving deeper into the primitive data type strings and the different ways we can manipulate them so this will get way more fun and practical! Thanks for reading, hope you enjoyed!