Lexical Structure of JavaScript

Shivshankar Pindoriya
Groww Engineering
Published in
3 min readNov 15, 2022

--

The lexical structure(lowest level syntax) of a programming language is the set of elementary rules that specifies how you write programs in that language.

We will try to understand about following topics:
1. case sensitivity, spaces, and line breaks
2. Literals
3. Identifiers
4. Reserved Words
5. Unicode
6. Optional Semicolons

Case sensitivity, spaces, and line breaks

JavaScript is a case-sensitive language i.e. groww, Groww, and GROWW all are distinct values in language and also ignore spaces and line breaks.

Literals

A literal is a data value that appears directly in a program. The following are all literal: (more details will be discussed on it in upcoming chapters)

12, 1.2, “hello world”, ‘Hi’, true, false, null

Identifiers

An identifier is a name or placeholder for a value that is used to name constants, variables, properties, functions, and classes and to provide labels for certain loops. An identifier must begin with a
letter, an underscore (_), or a dollar sign ($). digits are not allowed as the first character so that js can distinguish between identifiers and numbers.
Subsequent characters can be letters, digits, underscores, or dollar signs. These are all legal identifiers:

i, my_variable_name, v13, _dummy, $str

Reserved Words

JavaScript reserves certain identifiers for use by the language itself and these `Reserved Words` are part of the language and can’t be used as identifiers. A few examples of reserved words are const, if, else, try, and catch. The entire list of language-reserved words can be found on google easily.

Unicode

Unicode is universal character encoding used to process, store and facilitate the interchange of text data in any language. JavaScript programs are written using the Unicode character set.

Some computer hardware and software cannot display, input, or correctly process the full set of Unicode characters. To support programmers and systems using older technology, JavaScript defines escape sequences that allow us to write Unicode characters using only ASCII characters.
(ASCII character encoding is used for the representation of text such as symbols, letters, digits, etc. in computers)

These Unicode escapes begin with the characters \u
For eg café can be written in the following ways in JavaScript
* café with Unicode character é but might not work in older systems and technology.
* Caf\u00e9 with Unicode escape sequence<exactly four hexadecimal digits> using uppercase or lowercase letters A-F.
* caf\u{E9} another form of Unicode escape sequence<one to six hexadecimal digits enclosed within curly braces> introduced in ES6 to support Unicode codepoints which require more than 16 bits such as emoji.

Optional Semicolons

JavaScript uses the semicolon (;) to separate statements from one another.
you can usually omit the semicolon in language between two separate line statements or the end of the program or if the next token in the program is curly braces }.
A general rule for ‘when line breaks are considered as a semicolon: JavaScript treats a line break as a semicolon if the next nonspace character cannot be interpreted as a continuation of the current statement. Consider the following code:

let a
a
=
3
console.log(a)

JavaScript interprets the above code like this

let a; a = 3; console.log(a);

There are three exceptions to the general rule and JavaScript will always interpret a line break as a semicolon in these exceptions:
1. If a line break appears after any of these words (return, throw, yield, break, and continue).
2. The ++ and −− operators, These operators can be prefix operators that appear before an expression or postfix operators that appear after an expression.
3. The third exception involves functions defined using concise “arrow” syntax: the => arrow itself must appear on the same line as the parameter list

Next Steps

These next few posts will be a part of a series discussing basics and advanced topics on JavaScript.

Click here if you want to explore the blog series from the beginning.

If you’ve enjoyed this story, please click the 👏 button and share it, so that others can find it as well! Also, feel free to leave a comment below.

Groww Engineering publishes technical anecdotes, the latest technologies, and better ways to tackle common programming problems. You can subscribe here to get the latest updates.

We are hiring. View job openings here.

--

--