Javatpoint Logo
Javatpoint Logo

ES6 Syntax

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.

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:

  • Multi-line comments: These comments are used for commenting on the multiple lines.
    e.g.: /* It is an
    example of multi-line comment*/.
  • Single-line comments: These comments are used to comment on the single line.
    e.g.: // It is a single-line comment.

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:

  • Identifiers do not include any special symbol except the dollar sign ($) or underscore (_).
  • The identifier name must not be a keyword.
  • Identifiers include the characters as well as digits. But you cannot start the name of the identifier with a digit.
  • Identifiers are case-sensitive and do not contain spaces.

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.

There are some important points in the reference of JavaScript that are defined as follows:

JavaScript and Camel Case

JavaScript programmers should use the Lower Camel Case (starts with a lowercase letter).

e.g. userName, firstName, cityName, etc.

It is to be noted that hyphens are not allowed in JavaScript, because hyphens are reserved for subtractions.

JavaScript is Case-Sensitive

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

Semicolons are optional

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.

For example:

Execution of Code in JavaScript

Let us try to understand how code executes in JavaScript by using an example:

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:

There are several changes that strict mode makes to normal JavaScript semantics:

  • The strict mode prohibits some of the syntaxes, which is likely to be defined within the future versions of ECMAScript.
  • Strict mode removes some of the JavaScript silent errors by changing them to throw errors.
  • The strict mode also fixes the mistakes, which makes it difficult for the JavaScript engines to perform optimizations, so the strict mode sometimes makes to run faster compared to the identical 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.

For example:

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

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.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA