/

The Lexical Structure of JavaScript: Understanding the Building Blocks

The Lexical Structure of JavaScript: Understanding the Building Blocks

In this technical blog, we will delve into the fundamental components of JavaScript and explore their importance. We will cover topics such as Unicode, semicolons, white space, case sensitivity, comments, literals, identifiers, and reserved words.

Table of Contents

Unicode

JavaScript is written in Unicode, which allows the usage of characters from different languages and even emojis as variable names. However, there are rules to consider when using non-English characters in identifiers. You can find more details about these rules here.

Semicolons

JavaScript has a syntax that resembles the C programming language, leading to widespread usage of semicolons at the end of each line in code samples. However, it is important to note that semicolons are not mandatory in JavaScript. Many developers, especially those coming from languages that do not require semicolons, have started avoiding them. As long as you avoid typing statements on multiple lines or starting a line with parentheses, such as return followed by a new line and then a variable, you will be safe in omitting semicolons. Additionally, modern linters will warn you about potential issues with your code. On this site, we have adopted the practice of not adding useless semicolons.

White space

In JavaScript, white space is not considered meaningful. You can add spaces and line breaks in any manner you prefer. However, in practice, it is advisable to follow a consistent coding style. Utilizing tools like linters or formatting tools such as Prettier can help enforce a well-defined coding style. For example, many developers prefer using a two-space indentation.

Case sensitive

JavaScript is a case-sensitive language. This means that a variable named something is distinct from Something. The same rule applies to any identifier used in JavaScript.

Comments

JavaScript supports two types of comments:

  • /* */ - This type of comment can span multiple lines and needs to be closed.
  • // - This type of comment comments out everything on its right and continues until the end of the line.

Literals and Identifiers

In JavaScript, a literal is a value that is written directly in the source code. Examples of literals include numbers, strings, booleans, as well as more complex constructs like object literals and array literals. Here are some examples of literals:

1
2
3
4
5
5
'Test'
true
['a', 'b']
{color: 'red', shape: 'Rectangle'}

An identifier, on the other hand, is a sequence of characters that can be used to identify variables, functions, objects, and more. An identifier can start with a letter, a dollar sign $, or an underscore _. It can also include digits. Using Unicode, an identifier can even begin with an emoji, such as 😄. Here are some examples of identifiers:

1
2
3
4
5
6
Test
test
TEST
\_test
Test1
$test

The dollar sign is commonly used to reference DOM elements.

Reserved words

JavaScript has a set of reserved words that cannot be used as identifiers. Using these words as identifiers will result in an error. The following is a list of reserved words in JavaScript:

1
break, do, instanceof, typeof, case, else, new, var, catch, finally, return, void, continue, for, switch, while, debugger, function, this, with, default, if, throw, delete, in, try, class, enum, extends, super, const, export, import, implements, let, private, public, interface, package, protected, static, yield

Understanding these reserved words is crucial for avoiding conflicts in your code.

Tags: JavaScript, lexical structure, unicode, semicolons, white space, case sensitivity, comments, literals, identifiers, reserved words