ES6 - Syntax

Syntax defines the set of rules for writing programs. Every language specification defines its own syntax.
A JavaScript program can be composed of −
Variables − Represents a named memory block that can store values for the program.
Variables − Represents a named memory block that can store values for the program.
Literals − Represents constant/fixed values.
Literals − Represents constant/fixed values.
Operators − Symbols that define how the operands will be processed.
Operators − Symbols that define how the operands will be processed.
Keywords − Words that have a special meaning in the context of a language.
Keywords − Words that have a special meaning in the context of a language.
The following table lists some keywords in JavaScript. Some commonly used keywords are listed in the following table.
Modules − Represents code blocks that can be reused across different programs/scripts.
Modules − Represents code blocks that can be reused across different programs/scripts.
Comments − Used to improve code readability. These are ignored by the JavaScript engine.
Comments − Used to improve code readability. These are ignored by the JavaScript engine.
Identifiers − These are the names given to elements in a program like variables, functions, etc. The rules for identifiers are −

Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
Identifiers cannot be keywords. They must be unique.
Identifiers are case sensitive. Identifiers cannot contain spaces.


Identifiers − These are the names given to elements in a program like variables, functions, etc. The rules for identifiers are −
Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
Identifiers can include both, characters and digits. However, the identifier cannot begin with a digit.
Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
Identifiers cannot include special symbols except for underscore (_) or a dollar sign ($).
Identifiers cannot be keywords. They must be unique.
Identifiers cannot be keywords. They must be unique.
Identifiers are case sensitive. Identifiers cannot contain spaces.
Identifiers are case sensitive. Identifiers cannot contain spaces.
The following table illustrates some valid and invalid identifiers.
firstName
first_name
num1
$result
Var#
first name
first-name
1number
ES6 ignores spaces, tabs, and newlines that appear in programs. You can use spaces, tabs, and newlines freely in your program and you are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.
JavaScript is case-sensitive. This means that JavaScript differentiates between the uppercase and the lowercase characters.
Each line of instruction is called a statement. Semicolons are optional in JavaScript.
console.log("hello world") 
console.log("We are learning ES6") 
A single line can contain multiple statements. However, these statements must be separated by a semicolon.
Comments are a way to improve the readability of a program. Comments can be used to include additional information about a program like the author of the code, hints about a function/construct, etc. Comments are ignored by the compiler.
JavaScript supports the following types of comments −
Single-line comments (//) − Any text between a // and the end of a line is treated as a comment.
Single-line comments (//) − Any text between a // and the end of a line is treated as a comment.
Multi-line comments (/*   */) − These comments may span multiple lines.
Multi-line comments (/*   */) − These comments may span multiple lines.
//this is single line comment  
/* This is a  
Multi-line comment 
*/
Let us start with the traditional “Hello World” example".
var message = "Hello World" 
console.log(message)
The program can be analyzed as −
Line 1 declares a variable by the name message. Variables are a mechanism to store values in a program.
Line 1 declares a variable by the name message. Variables are a mechanism to store values in a program.
Line 2 prints the variable’s value to the prompt. Here, the console refers to the terminal window. The function log () is used to display the text on the screen.
Line 2 prints the variable’s value to the prompt. Here, the console refers to the terminal window. The function log () is used to display the text on the screen.
We shall use Node.js to execute our code.
Step 1 − Save the file as Test.js
Step 1 − Save the file as Test.js
Step 2 − Right-click the Test.js file under the working files option in the project-explorer window of the Visual Studio Code.
Step 2 − Right-click the Test.js file under the working files option in the project-explorer window of the Visual Studio Code.
Step 3 − Select Open in Command Prompt option.
Step 3 − Select Open in Command Prompt option.
Step 4 − Type the following command in Node’s terminal window.
Step 4 − Type the following command in Node’s terminal window.
node Test.js 

The following output is displayed on successful execution of the file.
Hello World

ECMAScript 2015(ES6) features are classified into three groups −
For Shipping − These are features that V8 considers stable.
For Shipping − These are features that V8 considers stable.
Staged Features − These are almost completed features but not considered stable by the V8 team.
Staged Features − These are almost completed features but not considered stable by the V8 team.
In Progress − These features should be used only for testing purposes.
In Progress − These features should be used only for testing purposes.
The first category of features is fully supported and turned on by default by node. Staged features require a runtime - - harmony flag to execute.
A list of component specific CLI flags for Node.js can be found here − https://nodejs.org/api/cli.html
The fifth edition of the ECMAScript specification introduced the Strict Mode. The Strict Mode imposes a layer of constraint on JavaScript. It makes several changes to normal JavaScript semantics.
The code can be transitioned to work in the Strict Mode by including the following −
// Whole-script strict mode syntax 
"use strict"; 
 v = "Hi!  I'm a strict mode script!";  // ERROR: Variable v is not declared
In the above snippet, the entire code runs as a constrained variant of JavaScript.
JavaScript also allows to restrict, the Strict Mode within a block’s scope as that of a function. This is illustrated as follows −
v = 15 
function f1() { 
   "use strict"; 
   var v = "Hi!  I'm a strict mode script!"; 
}
In, the above snippet, any code outside the function will run in the non-strict mode. All statements within the function will be executed in the Strict Mode.
The JavaScript engine, by default, moves declarations to the top. This feature is termed as hoisting. This feature applies to variables and functions. Hoisting allows JavaScript to use a component before it has been declared. However, the concept of hoisting does not apply to scripts that are run in the Strict Mode.
Variable Hoisting and Function Hoisting are explained in the subsequent chapters.

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    40 Lectures 
     5 hours 

 Richa Maheshwari

    16 Lectures 
     1 hours 

 Anadi Sharma

    50 Lectures 
     6.5 hours 

 Gowthami Swarna

    14 Lectures 
     1 hours 

 Deepti Trivedi

    31 Lectures 
     1.5 hours 

 Shweta
 Print
 Add Notes
 Bookmark this page