Unit 2: Emerging Javascript
Unit 2: Emerging Javascript
Emerging JavaScript
Contents
• Introduction
• Declaring Variables in ES6
• Arrow Function
• Transpiling ES6
• Objects and Arrays
• Promises
• Classes
• ES6 Modules
Introduction
• ECMAScript (ES) / ES6 is a scripting language specification
standardized by ECMAScript International.
• Used by applications to enable client-side scripting.
Languages like JavaScript, Jscript and ActionScript are
governed
• Since its release in 1995, JavaScript has gone through many
changes.
– Adding interactive elements to web pages much simpler
– Robust with DHTML and AJAX
– Node.js, JavaScript has become a language that is used to build full-
stack applications.
– The committee that is and has been in charge of shepherding the
changes to JavaScript is the European Computer Manufacturers
Association (ECMA).
• Changes to the language are community-driven by this
specification.
Setting Up ES6 Environment
• Install node.js
• Visual Studio Code
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.
– Literals − Represents constant/fixed values.
– Operators − Symbols that define how the operands will be processed.
– Keywords − Words that have a special meaning in the context of a
language.
• 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.
• 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.
• Whitespace and Line Breaks
– 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
– JavaScript is case-sensitive. This means that JavaScript differentiates between
the uppercase and the lowercase characters.
• Semicolons are Optional
– Each line of instruction is called a statement. Semicolons are optional
in JavaScript.
Sample Code
• 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 in JavaScript
• Functions may also return the value along with control, back to the
caller.
– Such functions are called as returning functions.
• Following is the syntax for the returning function.
function function_name()
{
//statements return value;
}
– A returning function must end with a return statement.
– A function can return at the most one value. In other words,
there can be only one return statement per function.
– The return statement should be the last statement in the
function.
• Functions may also return the value along with control, back to the
caller.
– Such functions are called as returning functions.
• Following is the syntax for the returning function.
function function_name()
{
//statements return value;
}
– A returning function must end with a return statement.
– A function can return at the most one value. In other words,
there can be only one return statement per function.
– The return statement should be the last statement in the
function.
Parameterized functions
• Parameters are a mechanism to pass values to functions.
• Parameters form a part of the function’s signature.
• The parameter values are passed to the function during its
invocation.
• Unless explicitly specified, the number of values passed to a function
must match the number of parameters defined.
function function_name(parameter1, paramerter2,…)
{
}
Template Strings
• Template strings provide us with an alternative to string
concatenation.
• They allow us to insert variables into a string.
• Traditional string concatenation uses plus sign to compose
a string using variable values and strings:
– console.log(lastName + ", " + firstName + " " +
middleName)
– With a template, we can create one string and insert the variable values by
surrounding them with ${ }:
• ES6 gives us new ways for working with objects and arrays and for
scoping the variables within these datasets.
• These features include
– Destructuring
– Object literal enhancement
– Spread operator
Destructuring Assignment
• The destructuring assignment allows you to locally scope fields
within an object and to declare which values will be used.
var sandwich =
{
bread: “soft",
cheese: “cheddar",
toppings: [“cucumber", "tomato", “pineapple"]
}
var {bread, cheese} = sandwich
console.log(bread, cheese)
The above shown code pulls bread and cheese out of the object and creates
local variables for them.
We can also change the values of the sandwich
structure:
var {bread, cheese} = sandwich
bread = "garlic"
cheese = “cottage cheese"
console.log(sandwich.bread, sandwich.cheese)
•Destructuring is also more declarative, meaning that our code
is more descriptive about what we are trying to accomplish
• We can also destructure incoming function arguments.
Consider this function that would log a person’s name as a
lord:
var lordify = regularPerson =>
{
console.log(`${regularPerson.firstname} of Canterbury`)
}
var regularPerson =
{ firstname: "Bill", lastname: "Wilson“ }
lordify(regularPerson)
• Instead of using dot notation syntax to dig into objects, we can
destructure the values that we need out of regularPerson:
var lordify =
({firstname}) =>
{ console.log(`${firstname} of Canterbury`) }
lordify(regularPerson)