Day -2 Blog writing

Some interesting things of ES6

European Computer Manufacturers Association (ECMAScript) or (ES) is a standard for scripting languages like JavaScript, ActionScript and JScript. It was initially created to standardize JavaScript, which is the most popular implementation of ECMAScript. This tutorial adopts a simple and practical approach through JavaScript to describe the new features in ECMAScript 2015 (ES6), ECMAScript 2016 (ES7), ECMAScript 2017(ES8) and ECMAScript 2018 (ES9). ECMAScript 6 was the second major revision to JavaScript. ECMAScript 6 is also known as ES6 and ECMAScript 2015.

New Features in ES6

  1. The let keyword
  2. The const keyword
  3. Arrow Functions
  4. For/of
  5. Classes
  6. Promises
  7. Symbol
  8. Default Parameters
  9. Function Rest Parameter
  10. Array.find()
  11. Array.findIndex()
  12. New Math Methods
  13. New Number Properties
  14. New Number Methods
  15. New Global Methods
  16. Modules

Introduction to Scope

What is Scope?

Scope determines the visibility or accessibility of a variable or other resource in the area of your code.

Global Scope

The area outside all the functions is consider the global scope and the variables defined inside the global scope can be accessed and altered in any other scopes.

Local Scope

Variables declared inside the functions become Local to the function and are considered in the corresponding local scope. Every Functions has its own scope.

Function Scope

Whenever you declare a variable in a function, the variable is visible only within the function. You can't access it outside the function.

Block Scope

A block scope is the area within if, switch conditions or for and while loops. Generally speaking, whenever you see curly brackets, it is a block.

Commenting in JavaScript

The JavaScript comment feature simplifies the production of more readable code. Comments in JavaScript are used to explain the code and make the program more readable for developers.

Check out the example of commenting out a single-line below:

// This is single line comment. 
console.log("hello world");

Check out the example of commenting out a multi-line below:

/* const x = 50; 
   const y = 60;
   const sum = x+y;*/

Destructuring in JavaScript

ES6 provides a new feature called destructing assignment that allows you to destructure properties of an object or elements of an array into individual variables. Destructuring means to break down a complex structure into simpler parts.

Array destructure

const colors = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange", "Red"];  

// destructuring assignment  
const[color1, color2, color3] = colors;  

console.log(color1); // Violet  
console.log(color2); // Indigo  
console.log(color3); // Blue

Object destructure

const Student = {
    firstName: 'Krunal',
    lastName: 'Lathiya',
    university: 'GTU',
    enrollno: 110470116021
};
const { firstName, lastName, university, enrollno } = Student;
console.log(firstName);
console.log(lastName);
console.log(university);
console.log(enrollno);

Error Handling

There are three types of errors in programming: a. Syntax Errors b. Runtime Errors c. Logical Errors.

Syntax Errors

Syntax errors, also called parsing errors, occur at compile time in traditional programming languages and at interpret time in JavaScript.

Runtime Errors

Runtime errors, also called exceptions, occur during execution.

Logical Errors

Logic errors can be the most difficult type of errors to track down. These errors are not the result of a syntax or runtime error. Instead, they occur when you make a mistake in the logic that drives your script and you do not get the result you expected.

How to handle this errors?

The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.

<script type = "text/javascript">
   <!--
      try {
         // Code to run
         [break;]
      } 

      catch ( e ) {
         // Code to run if an exception occurs
         [break;]
      }

      [ finally {
         // Code that is always executed regardless of 
         // an exception occurring
      }]
   //-->
</script>