Advanced Javascript: Arrow Functions, Template Literals, Array Destructing
Advanced Javascript: Arrow Functions, Template Literals, Array Destructing
GMRIT/ADM/F-44
Rajam, Andhra Pradesh
REV.: 00
(An Autonomous Institution Affiliated to JNTUGV, AP)
❖ Objective
❖ Apply dynamic effects and validations using JavaScript skills to real-world projects
❖ Demonstrating the ability to build interactive and dynamic web applications
❖ Evocation
❖ Deliverables
Lecture - 6
➢ Arrow functions
❖ Arrow function {()=>} is concise way of writing JavaScript functions in shorter way.
Arrow functions were introduced in the ES6 version. They make our code more
structured and readable.
❖ Arrow functions are anonymous functions i.e. functions without a name but they
are often assigned to any variable. They are also called Lambda Functions.
❖ Example
o Arrow Function without Parameters
hello = () => {
return "Hello World!";
}
Result:
Hello World!
values( 10, 20 );
Result:
GMRIT, Rajam, Andhra Pradesh
10 20 30
o Arrow functions can be async by prefixing the expression with the async
keyword.
async param => expression
async (param1, param2, ...paramN) => {
statements
}
o Advantages of Arrow Functions
- Arrow functions reduce the size of the code.
- The return statement and function brackets are optional for single-line
functions.
- It increases the readability of the code.
- Arrow functions provide a lexical this binding. It means, they inherit the
value of “this” from the enclosing scope. This feature can be advantageous
when dealing with event listeners or callback functions where the value of
“this” can be uncertain.
o Limitations of Arrow Functions
- Arrow functions do not have the prototype property.
- Arrow functions cannot be used with the new keyword.
- Arrow functions cannot be used as constructors.
- These functions are anonymous and it is hard to debug the code.
- Arrow functions cannot be used as generator functions that use the yield
keyword to return multiple values over time.
Lecture - 7
➢ Template Literals (Template Strings)
❖ Template literals (template strings) allow you to use strings or embedded
expressions in the form of a string. They are enclosed in backticks ``. For example,
const name = 'Jack';
console.log(`Hello ${name}!`); // Hello Jack!
➢ Array Destructing
❖ Destructuring means to break down a complex structure into simpler parts. With
the syntax of destructuring, you can extract smaller fragments from objects and
arrays. It can be used for assignments and declaration of a variable.
❖ Destructuring is an efficient way to extract multiple values from data that is stored
in arrays or objects. When destructuring an array, we use their positions (or index)
in an assignment.
❖ Let us try to understand the array destructuring by using some illustrations:
var arr = ["Hello", "World"]
// destructuring assignment
var [first, second] = arr;
console.log(first); // Hello
console.log(second); // World
Result:
Hello
World
❖ By using the rest operator (…) in array destructuring, you can put all the remaining
elements of an array in a new array.
❖ Let us try to understand it with an example.
var colors = ["Violet", "Indigo", "Blue", "Green", "Yellow", "Orange"];
❖ First, try to understand the basic assignment in object destructuring by using the
following example.
❖ Example:
const num = {x: 100, y: 200};
const {x, y} = num;
console.log(x); // 100
console.log(y); // 200
Result:
100
200
❖ Object destructuring and default values
o Like array destructuring, a default value can be assigned to the variable if the
value unpacked from the object is undefined. It can be clear from the following
example.
const {x = 100, y = 200} = {x: 500};
console.log(x); // 500
console.log(y); // 200
Result:
500
200
❖ Before ES6, it was hard to create a class in JavaScript. But in ES6, we can create the
class by using the class keyword. We can include classes in our code either by class
expression or by using a class declaration.
❖ A class definition can only include constructors and functions. These components
are together called as the data members of a class. The classes contain constructors
that allocates the memory to the objects of a class. Classes contain functions that
are responsible for performing the actions to the objects.
❖ Syntax: Class Declaration
class Class_name{
}
❖ Syntax: Class Expression
var var_name = new class_name {
}
❖ Let us see the illustration for the class expression and class declaration.
❖ Example - Class Declaration
class Student{
constructor(name, age){
this.name = name;
this.age = age;
}
}
➢ Modules
❖ Modules are the piece or chunk of a JavaScript code written in a file. JavaScript
modules help us to modularize the code simply by partitioning the entire code into
modules that can be imported from anywhere. Modules make it easy to maintain
the code, debug the code, and reuse the piece of code. Each module is a piece of
code that gets executed once it is loaded.
❖ Modules in ES6 is an essential concept. Although it is not available everywhere, but
today we can use it and can transpile into ES5 code. Transpilation is the process of
converting the code from one language into its equivalent language. The ES6
module transpiler tool is responsible for taking the ES6 module and converts it into
a compatible code of ES5 in the AMD (Asynchronous module definition is a
specification for the JavaScript programming language) or in the CommonJS style.
❖ Exporting a Module
o JavaScript allows us to export a function, objects, classes, and primitive values
by using the export keyword. There are two kinds of exports:
o Named Exports: The exports that are distinguished with their names are called
as named exports. We can export multiple variables and functions by using the
named export.
o Default Exports: There can be, at most, one value that can be exported by using
the default export.
❖ Importing a Module
o To import a module, we need to use the import keyword. The values which are
exported from the module can be imported by using the import keyword. We
can import the exported variables, functions, and classes in another module. To
import a module, we simply have to specify their path.
o When you import the named export, you must have to use the same name as the
corresponding object. When you import the default export, we can use any
name of the corresponding object.
❖ Sample Questions
Remember
1. Define Fuctions.
2. List out advantages of JavaScript
Understanding
At the end of this session, the facilitator (Teacher) shall randomly pic-up few students to
summarize the deliverables.