Most statements in JavaScript must end with a semicolon (;
). However, JavaScript engines typically feature Automatic Semicolon Insertion (ASI). This means if you omit a semicolon, the engine will automatically insert one where it deems appropriate.
The behavior of Automatic Semicolon Insertion (ASI) can vary between different implementations, potentially leading to code interpretation that doesn’t match the programmer’s intent. Therefore, my personal preference is to always end statements with a semicolon. Most JavaScript linters, such as Prettier, prefer the addition of semicolons.
Identifiers (names of any function, property, or variable) may contain letters (only ASCII letters so thankfully you can’t use an emoji as a variable name), numbers, dollar signs, or underscores. Here are some other considerations:
$
or _
as the first character of your variables. (Why? For clarity mainly! Some popular JavaScript libraries (such as jQuery, UnderscoreJS, Lodash) use these characters as their identifiers.)Like most programming languages, there are a number of reserved words in JavaScript that you cannot use to name your functions and variables (e.g. var
, let
, new
). For a complete list, refer to MDN web docs; JavaScript reference: Keywords.
Comments in JavaScript are similar to Java and C++: