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.
The program within the JavaScript consists of:
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.
int x = 1; string str = "hello";
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.
JavaScript supports the following two types of comments:
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.
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.
There are some important points in the reference of JavaScript that are defined as follows:
JavaScript programmers should use the Lower Camel Case (starts with a lowercase letter).
e.g. userName, firstName, cityName, etc.
The identifiers in JavaScript are case-sensitive. It means that uppercase characters and lowercase characters are different in JavaScript.
For example:
username and userName both are the different variables in JavaScript
var username, userName; username = "Anil"; userName = "Sunil";
The use of semicolons is optional in JavaScript. But if a single line has multiple statements (sequence of instructions) then, these statements must be separated by the semicolons.
console.log("hello world") console.log("Welcome to rookienerd.com"); console.log("You are learning the ES6 tutorial");
Execution of Code in JavaScript
Let us try to understand how code executes in JavaScript by using an example:
var msg = "Hello World" //This line declares a variable. console.log(msg); //This line print the value of the variable to the prompt. The console in it refers to the terminal window, and the log() is used to display the text on the screen.
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 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.
There are several changes that strict mode makes to normal JavaScript semantics:
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.
"use strict"; example(); function example() { x= 89; // It will also cause an error because x is not declared
}
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.
y = 89; // It will not cause an error. example(); function example() { "use strict"; x = 89; // It will cause an error }
Hoisting is the default behavior to move all of the declarations at the top of the scope before the execution of code. It applies to functions and variables. It allows the JavaScript to use the component before its declaration. Hoisting does not apply to scripts that run in strict mode.