ES6 - Tutorialspoint

[Pages:435]ES6

ES6

About the Tutorial

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

Audience

This tutorial is designed for the stemplate oftware programmers who have already worked with JavaScript and wishes to gain in-depth knowledge about the ECMAScript. The tutorial will give you enough understanding on the functionalities of ECMAScript and also about ES6, ES7, ES8 and ES9.

Prerequisites

An understanding of JavaScript programming concepts is necessary to gain maximum knowledge from this tutorial.

Disclaimer & Copyright

Copyright 2019 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@.

i

ES6

Table of Contents

About the Tutorial....................................................................................................................................i Audience ..................................................................................................................................................i Prerequisites ............................................................................................................................................i Disclaimer & Copyright.............................................................................................................................i Table of Contents ....................................................................................................................................ii

1. ES6 - OVERVIEW .................................................................................................................1

JavaScript ................................................................................................................................................1 ECMAScript Versions ...............................................................................................................................1

2. ES6 - ENVIRONMENT ..........................................................................................................3

Local Environment Setup.........................................................................................................................3 Installation on Windows .........................................................................................................................3 Installation on Mac OS X .........................................................................................................................4 Installation on Linux................................................................................................................................5 Integrated Development Environment (IDE) Support ..............................................................................5 Visual Studio Code ..................................................................................................................................5 Brackets ..................................................................................................................................................8

3. ES6 - SYNTAX ....................................................................................................................11

Whitespace and Line Breaks..................................................................................................................12 Comments in JavaScript ........................................................................................................................12 Your First JavaScript Code .....................................................................................................................13 Executing the Code................................................................................................................................13 Node.js and JS/ES6 ................................................................................................................................14 The Strict Mode.....................................................................................................................................14 ES6 and Hoisting....................................................................................................................................15

ii

ES6

4. ES6 - VARIABLES ...............................................................................................................16

Type Syntax...........................................................................................................................................16 JavaScript and Dynamic Typing .............................................................................................................17 JavaScriptVariable Scope.......................................................................................................................17 The Let and Block Scope........................................................................................................................18 let and block level safety.......................................................................................................................19 let and multiple blocks ..........................................................................................................................19 The const...............................................................................................................................................20 Constants are Immutable ......................................................................................................................21 const and arrays....................................................................................................................................21 The var keyword ...................................................................................................................................22 var and hoisting ....................................................................................................................................22 var and block scope...............................................................................................................................23 var and block level safety ......................................................................................................................23

5. ES6 - OPERATORS .............................................................................................................25

Arithmetic Operators ............................................................................................................................25 Relational Operators .............................................................................................................................27 Logical Operators ..................................................................................................................................28 Bitwise Operators .................................................................................................................................29 Assignment Operators...........................................................................................................................31 Miscellaneous Operators ......................................................................................................................32

6. ES6 - DECISION MAKING ...................................................................................................37

The if Statement....................................................................................................................................37 The if...else Statement...........................................................................................................................39 The else...if Ladder ................................................................................................................................40 The switch...case Statement ..................................................................................................................41

iii

ES6

7. ES6 - LOOPS ......................................................................................................................45

Definite Loop.........................................................................................................................................45 Indefinite Loop......................................................................................................................................48 The Loop Control Statements ................................................................................................................51 Using Labels to Control the Flow ...........................................................................................................52

8. ES6 - FUNCTIONS..............................................................................................................55

Classification of Functions .....................................................................................................................55 Rest Parameters....................................................................................................................................60 Anonymous Function ............................................................................................................................60 The Function Constructor ......................................................................................................................61 Recursion and JavaScript Functions.......................................................................................................62 Lambda Functions .................................................................................................................................63 Function Expression and Function Declaration ......................................................................................64 Function Hoisting ..................................................................................................................................65 Immediately Invoked Function Expression ............................................................................................65 Generator Functions .............................................................................................................................67 Arrow Functions....................................................................................................................................68 Arrow Function Syntax ..........................................................................................................................69 Array.prototype.map() and arrow function...........................................................................................71 Arrow function and "this" .....................................................................................................................72

9. ES6 - EVENTS ....................................................................................................................74

Event Handlers ......................................................................................................................................74 onclick Event Type.................................................................................................................................74 onsubmitEvent Type .............................................................................................................................75 onmouseover and onmouseout ............................................................................................................76 HTML 5 Standard Events .......................................................................................................................76

iv

ES6

10. ES6 - COOKIES...................................................................................................................83

How It Works?.......................................................................................................................................83 Storing Cookies .....................................................................................................................................83 Reading Cookies ....................................................................................................................................85 Setting Cookies Expiry Date...................................................................................................................86 Deleting a Cookie ..................................................................................................................................87

11. ES6 - PAGE REDIRECT........................................................................................................89

JavaScript Page Redirection ..................................................................................................................89 Redirection and Search Engine Optimization.........................................................................................90

12. ES6 - DIALOG BOXES .........................................................................................................91

Alert Dialog Box ....................................................................................................................................91 Confirmation Dialog Box .......................................................................................................................92 Prompt Dialog Box ................................................................................................................................93

13. ES6 - VOID KEYWORD .......................................................................................................95

Void and Immediately Invoked Function Expressions............................................................................95 Void and JavaScript URIs .......................................................................................................................95

14. ES6 - PAGE PRINTING........................................................................................................97

15. ES6 - OBJECTS ...................................................................................................................98

Object Initializers ..................................................................................................................................98 The Object() Constructor .......................................................................................................................99 Constructor Function...........................................................................................................................101 The Object.create Method ..................................................................................................................103 The Object.assign() Function ...............................................................................................................103 Deleting Properties .............................................................................................................................105 Comparing Objects..............................................................................................................................105

v

ES6

Object De-structuring..........................................................................................................................106

16. ES6 - NUMBER ................................................................................................................110

Number Properties..............................................................................................................................110 EPSILON ..............................................................................................................................................111 MAX_SAFE_INTEGER ...........................................................................................................................111 MAX_VALUE ........................................................................................................................................ 111 MIN_SAFE_INTEGER ............................................................................................................................ 112 MIN_VALUE......................................................................................................................................... 112 Nan .....................................................................................................................................................113 NEGATIVE_INFINITY ............................................................................................................................113 POSITIVE_INFINITY .............................................................................................................................. 114 Number Methods................................................................................................................................114 Number.isNaN() ..................................................................................................................................115 Number.isFinite................................................................................................................................... 115 Number.isInteger() .............................................................................................................................. 116 Number.isSafeInteger() .......................................................................................................................116 Number.parseInt() ..............................................................................................................................117 Number.parseFloat() ...........................................................................................................................117 Number Instances Methods ................................................................................................................118 toExponential() ...................................................................................................................................118 toFixed() .............................................................................................................................................. 119 toLocaleString() ...................................................................................................................................120 toPrecision() ........................................................................................................................................ 120 toString() ............................................................................................................................................. 121 valueOf() .............................................................................................................................................122 Binary and Octal Literals .....................................................................................................................122

vi

ES6

Object literal Extension .......................................................................................................................123

17. ES6 - BOOLEAN ...............................................................................................................126

Boolean Properties..............................................................................................................................126 Boolean Methods................................................................................................................................128 toSource () ..........................................................................................................................................128 toString () ............................................................................................................................................129 valueOf () ............................................................................................................................................130

18. ES6 - STRINGS .................................................................................................................131

String Properties .................................................................................................................................131 Constructor .........................................................................................................................................131 Length .................................................................................................................................................132 Prototype ............................................................................................................................................132 String Methods ...................................................................................................................................133 charAt .................................................................................................................................................134 charCodeAt() .......................................................................................................................................135 concat() ...............................................................................................................................................136 indexOf() .............................................................................................................................................137 lastIndexOf() .......................................................................................................................................137 localeCompare() .................................................................................................................................. 138 replace() .............................................................................................................................................. 139 search() ...............................................................................................................................................140 slice() ..................................................................................................................................................141 split() ................................................................................................................................................... 142 substr() ................................................................................................................................................ 142 substring() ........................................................................................................................................... 143 toLocaleLowerCase() ...........................................................................................................................144

vii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download