Unit One ES5
Unit One ES5
var
var x = 5.6;
ES6, there are three ways of defining your variables: var, let, and const
let
let x = 5.6;
let is the block scoped version of var, and is limited to the block (or expression)
where it is defined.
If you use let inside of a block, i.e. a for loop, the variable is only available inside of
that loop.
let has a block scope.
const
const x = 5.6;
const is a variable that once it has been created, its value can never
change.
The object can access the attributes and functions of a class. We use the '.' dot
notation (or period) for accessing the data members of the class.
Syntax
1.obj.function_name();
1.class Student {
2. constructor(name, age) {
3. this.n = name;
4. this.a = age;
5. }
6. stu() {
7. console.log("The Name of the student is: ", this.n)
8. console.log("The Age of the student is: ",this. a)
9. }
10.}
11.
12.var stuObj = new Student('Peter',20);
13.stuObj.stu();
Classes
Example
A simple class constructor:
class Car { constructor(name) {
this.brand = name;
}
}
Create an object called "mycar" based on the Car class:
you call the method by referring to the object's method name followed by
parentheses
The Static keyword
The static keyword is used for making the static functions in the class.
Static functions are referenced only by using the class name.
Example
1.'use strict'
2.class Example {
3. static show() {
4. console.log("Static Function")
5. }
6.}
•Optional braces for single statement and the empty braces if there is not any parameter required.
1.var show = () => console.log("Hello World");
2.show();
Before:
hello = function() {
return "Hello World!"; }
Arrays are indexed from 0. The array name followed by the subscript
is used for referring an array element.
Syntax
1.array_name[subscript];
Example
1.var num;
2.num = [2,4,6,8];
3.console.log(num[0]);
4.console.log(num[1]);
Array Constructor
You create an array by using the array constructor. Array constructor can be
passed as:
•A list of values separated by comma or,
•A numeric value which indicates the size of array
Example - Single Numeric Value
1.var num = new Array(5); // This single numeric value indicates the siz
e of array.
2.var i;
3.for(i=0;i<num.length;i++){ Example - Comma Separated Values
4.num[i]=i*5; 1.var num = new Array(1,2,3,4,5);
5.console.log(num[i]); 2.var i;
6.} 3.for(i=0;i<num.length;i++){
4.console.log(num[i]);
5.}
ES6 Multidimensional Arrays
Declaration
The following syntax illustrates you how to declare two-dimensional array
in JavaScript.
1.var array_name = [[value1,value2,value3],[val1,val2,val3]];
2.function show(rainbow) {
var rainbow = new Array("Violet", "Indigo", "Blue",
3. for(var i = 0;i<rainbow.length;i++) {
"Green", "Yellow", "Orange", "Red");
4. console.log(rainbow[i])
var show =(rainbow)=>{
5. }
for(var i = 0;i<rainbow.length;i++)
6.}
{
7.show(rainbow)
console.log(rainbow[i])
}
}
show(rainbow)
Return Array from function
1.function show() {
2. return new Array("Blue", "Red", "Green", "Yellow")
3. }
4. var colors = show()
5. for(var i in colors) {
6. console.log(colors[i])
7. }
ES6 Array methods
The array methods introduced in ES6 are tabulated below.
3. Array.prototype.copyWithi It copies the part of an array to a different location within the same array.
n()
4. Array.prototype.find() It finds a value from an array, based on the specific criteria that are passed to
this method.
5. Array.prototype.findIndex( The Array.prototype.findIndex() returns the index of the first element of the
) given array that satisfies the given condition.
6. Array.prototype.entries() It returns an array iterator object, which can be used to loop through keys
and values of arrays.
7. Array.prototype.keys() It returns an array iterator object along with the keys of the array.
console.log(name)
console.log(name.length)
)
Array.copyWithin()
This method copies the part of an array to a different location within the same array.
It returns the modified array without any modification in its length.
Syntax
1.array.copyWithin(target, start, end)
It finds a value from an array, based on the specific criteria that are passed to this method.
It returns the first element value that satisfies the given condition.
map() creates a new array from calling a function for every array element.
map() calls a function once for each element in an array.
map() does not execute the function for empty elements.
map() does not change the original array.
console.log(map1);
With destructuring:
2.
3.// destructuring assignment
4.var[color1, color2, color3] = colors;
5.
6.console.log(color1); // Violet
7.console.log(color2); // Indigo
8.console.log(color3); // Blue
Example
If you want to choose random elements from the given array then in array
destructuring you can perform it as follows:
If you are taking a value from the array and that value is undefined, then you can assign a
default value to a variable.
1.var x, y;
2.
3.Var [x=50, y=70] = [100];
4.console.log(x); // 100
5.console.log(y); // 70
Swapping Variables
The values of the two variables can be swapped in one destructuring
expression. The array destructuring makes it easy to swap the values of
variables without using any temporary variable.
Example
1.function array() {
2. return [100, 200, 300];
3.}
4.
5.var [x, y, z] = array();
6.
7.console.log(x); // 100
8.console.log(y); // 200
9.console.log(z); // 300
function calculate(a, b) {
const add = a + b;
const subtract = a - b;
const multiply = a * b;
const divide = a / b;
Syntax
1.var variablename1 = [...value];
Constructing array literal
When we construct an array using the literal form, the spread operator
allows us to insert another array within an initialized array.
Example
1.let colors = ['Red', 'Yellow'];
2.let newColors = [...colors, 'Violet', 'Orange', 'Green'];
3.console.log(newColors);
Concatenating arrays
const numbersOne = [1, 2, 3];
const numbersTwo = [4, 5, 6];
const numbersCombined = [...numbersOne, ...numbersTwo];
console.log(numbersCombined);
Copying an array
Example
Without using spread operator
1.let colors = ['Red', 'Yellow'];
1.let colors = ['Red', 'Yellow']; 2.let newColors = colors;
2.let newColors = [...colors]; 3.newColors.push('Green');
3.console.log(newColors); 4.console.log(newColors);
5.console.log(colors);
Using spread operator
1.let colors = ['Red', 'Yellow'];
2.let newColors = [...colors];
3.newColors.push('Green');
4.console.log(newColors);
5.console.log(colors);
If we copy the array elements without using the spread operator, then inserting a new element
to the copied array will affect the original array.
But if we are copying the array by using the spread operator, then inserting an element in the
copied array will not affect the original array.
Destructuring Objects
const vehicleOne = {
const vehicleOne = {
brand: 'Ford',
brand: 'Ford',
model: 'Mustang',
model: 'Mustang',
type: 'car',
type: 'car',
year: 2021,
year: 2021,
color: 'red'
color: 'red'
}
}
myVehicle(vehicleOne);
myVehicle(vehicleOne);
// old way
function myVehicle({type, color, brand, model}) {
function myVehicle(vehicle) {
const message = 'My ' + type + ' is a ' + color + ' ‘
const message = 'My ' + vehicle.type + ' is a ‘
+ brand + ' ' + model + '.';
+ vehicle.color + ' ' + vehicle.brand + ‘
}
' + vehicle.model + '.';
}