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.

Example
snippet
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:

  • 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.

Note
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

Example
snippet
var username, userName;
username = "Anil";
userName = "Sunil";

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.

Example
snippet
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:

Example
snippet
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:

C:\. Command Prompt
node Example.js

After the successful execution, you will get the following output:

Output
Hello World

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.

Example
snippet
"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.

Example
snippet
y = 89;       // It will not cause an error.
example();
function example() {
  "use strict";
  x = 89;   // It will cause an error
}

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.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +