ES6 SyntaxThe 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:
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 WhitespacesECMAScript 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 CaseJavaScript 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-SensitiveThe 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 optionalThe 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 ModeThe 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:
How to declare the strict modeYou 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 HoistingHoisting 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. Next TopicES6 Spread Operator |