The syntax is the set of rules which defines the arrangements of symbols. Every language specification has its syntax. Syntax applies to programming languages in which the document represents the source code and also applies to markup languages , in which the document describes the data.
Literals: A literal can be defined as a notation for representing the fixed value within the source code. Generally, literals are used for initializing the variables. In the following example, you can see the use of literals in which 1 represents the integer literal, and the string "hello" is a stringliteral.
Variables: A variable is the storage location that is identified by the memory address. The variable is the name of a memory block that stores the values for the program. The name of the variable is the standard way of referencing the stored value.
Keywords:I n Computer programming, a keyword is a word that has a special meaning in a specific context. It cannot be used as an identifier like the variable name, function name, or label.
Operators: Operators are symbols that define the processing of operands. Some of the common examples of operators include arithmetic (addition with +), logical operators (like AND or &&), etc.
Comments: Comments are the programmer-readable annotations (extra information) in the source code of a computer program. Comments are added to make the source code easy to understand for humans. Comments increased the readability of code and ignored by the compilers and interpreters.
Identifiers: Identifiers are the names given to elements within a program, such as functions, variables, etc. There are some rules for the identifiers that are as follows:
For example: In this example, you will see the example of a valid and invalid declaration of identifiers.
Valid Identifiers Example: userName, user_name, name14, $name.
Invalid identifiers Example: Name@, user name, user-name, 14name.
Line Breaks and Whitespaces
ECMAScript ignores the tabs, spaces, and the newlines that appear in the programs. We can use the tabs, spaces, and newlines easily in the program and free to format and indent the program in a reliable manner, which increases the readability of the code and makes it easy to understand.
e.g. userName, firstName, cityName, etc.
Semicolons are optional
Save this file by using .js extension as we are saving it with name Example.js. Then, right-click on this file, which is placed under the working files option in the project explorer window of the visual studio code, and select the 'open in terminal' option.
Type the following command in a terminal for executing the file:
After the successful execution, you will get the following output:
The Strict Mode
The strict mode was introduced in ECMAScript 5 (the fifth edition of the ECMAScript specification). You can use this strict mode in all of your programs. It will help you to write clean code, like preventing you from using undeclared variables.
Advantages of using strict mode:
How to declare the strict mode
You can declare the strict mode by adding "use strict"; at the beginning of a function or a script.
Declare at the beginning of the script:When you declare it at the beginning of the script then, it will be a global scope, i.e., all code within the script will execute in the strict mode.
When we execute this example, we will get an error because we have not declared the variable x.
Declare inside the function: When you declare it inside the function, then it will be a local scope, i.e., the code within the function will be in strict mode.
ES6 and Hoisting