JS Advance Features
JS Advance Features
console.log(`Hi am ${name}
I play as a${role}
in this Team`)
TEMPLATE STRING(EXAMPLE)
Tagged Template
Are the more advanced form of template literals. With
tagged templates, we can parse template literals with a
function.
Syntax
for (variable of iterable){
// code block to be executed
}
Example
const players = [“Rahul", “Kuldeep", “Surya“,”Rothit”];
let team = "";
for (let player of players) {
team += x + " ";
}
console.log(‘Today Team Is : ‘+team)
SPREAD & REST SYNTAX
Rest and spread syntax use … (three dots) notation.
Spread syntax expands an array into separate
elements, while a rest syntax condenses array
elements into a single element.
Spread Syntax
function addnumbers(a,b,c){
return a+b+c
}
let numbers=[10,20,30]
let sum=addnumbers(...numbers)
console.log("Sum Of numbers : "+sum)
Old Syntax(ES-5)
var x = function(x, y) {
return x * y
}
New Syntax(ES-6)
const x = (x, y) => x * y
POINTS TO REMEMBER
Arrow functions do not have their own this.
Array Destructuring
Object Destructuring
DESTRUCTRING IN JS
Array Destructuring
let numbers=[10,20,30,40,50]
let [firstnumber]=numbers
console.log(firstnumber) //returns 10
DESTRUCTRING IN JS
With Multiple Elements
You can also extract multiple elements from an
array and assign them to multiple variables
let numbers=[10,20,30,40,50]
let [first,second,third]=numbers
console.log(first,second,third) //returns
10 20 30
let
car={brand:”Honda”,color:”Red”,price:1240000}
let {brand}=car
console.log(brand)
DESTRUCTRING IN JS
Nested Object Destructuring
String.startsWith()
let string = “Welcome to Full Stack";
string.startsWith("Hello")
String.endsWith()
let string = “Full Stack Is Powerful Tool";
string.startsWith(“Full")
ARRAY NEW FUNCTIONS
Array.from()
method returns an Array object from any
object with a length property or any iterable
object.
Array.from("ABCDEFG") // Returns [A,B,C,D,E,F,G]
Array.keys()
method returns an Array Iterator object with
the keys of an array.
const fruits =["Banana", "Orange", "Apple"];
const keys = fruits.keys();
let text = "";
for (let x of keys) {
text += x + "<br>";
}
ARRAY NEW FUNCTIONS
Array.find()
method returns the value of the first array
element that passes a test function
const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
Array.findIndex()
method returns the index of the first array
element that passes a test function.
const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
return value === 18;
}
ES 2021 FEATURES
String.replaceAll()
text=“all the Cats beign Cats afterall”;
text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
text=text.replaceAll(/Cats/g,”Dogs”)
ES 2021 FEATURES
JS Numeric_Seperator
ES2021 intoduced the numeric separator (_) to
make numbers more readable
const num=31_12_223
The numeric separator is only for visual use
const num1 = 1_000_000_000;
const num2 = 1000000000;
Console.log(num1 === num2);
<p id="demo"></p>
<script>
const temp = [27, 28, 30, 40, 42, 35, 30];
let high = temp.findLast(x => x > 40);
document.getElementById("demo").innerHTML
= "The last temperature over 40 was " + high;
</script>
ES 2023 FEATURES
Array.findLastIndex() method
The findLastIndex() method finds the index of the
last element that satisfies a condition
<p id="demo"></p>
<script>
const temp = [27, 28, 30, 40, 42, 35,52,
30];
let pos = temp.findLastIndex(x => x > 40);
document.getElementById("demo").innerHTML
= "The last temperature over 40 was in position
" + pos;
</script>
ES 2023 FEATURES
Array.toReversed() method
ES2023 added the Array toReversed() method as a safe
way to reverse an array without altering the original array.
The difference between the new toReversed() method
and the old reverse() method is that the new method
creates a new array, keeping the original array
unchanged, while the old method altered the original array
<p id="demo"></p>
<script>
const months = ["Jan", "Feb", "Mar", "Apr"];
const reversed = months.toReversed();
document.getElementById("demo").innerHTML
= reversed;
</script>
ES 2023 FEATURES
Array.toSorted() method
ES2023 added the Array toSorted() method as a safe
way to sort an array without altering the original array.
The difference between the new toSorted() method
and the old sort() method is that the new method
creates a new array, keeping the original array
unchanged, while the old method altered the original
array
<p id="demo"></p>
<script>
const months = ["Jan", "Feb", "Mar", "Apr"];
const sorted = months.toSorted();
document.getElementById("demo").innerHTML = sorted;
</script>
ES 2023 FEATURES
Array.toSpliced() method
ES2023 added the Array toSpliced() method as a safe
way to splice an array without altering the original array.
The difference between the new toSpliced() method and
the old splice() method is that the new method creates a
new array, keeping the original array unchanged, while
the old method altered the original array.
<p id="demo"></p>
<script>
const months = ["Jan", "Feb", "Mar", "Apr"];
const spliced = months.toSpliced(0, 1);
document.getElementById("demo").innerHTML
= spliced;
</script>
ES 2024 FEATURES
Object.groupBy() method
<p id="demo"></p>
<script>
const fruits = [
{name:"apples", quantity:300},
{name:"bananas", quantity:500},
{name:"oranges", quantity:200},
{name:"kiwi", quantity:150} ];
function myCallback({ quantity }) {
return quantity > 200 ? "ok" : "low";
}
const result = Object.groupBy(fruits, myCallback);
let text ="These fruits are Ok: <br>";
for (let [x,y] of result.ok.entries()) {
text += y.name + " " + y.quantity + "<br>";
}
text += "<br>These fruits are low: <br>";
for (let [x,y] of result.low.entries()) {
text += y.name + " " + y.quantity + "<br>";
}
document.getElementById("demo").innerHTML = text;
The</script>
Object.groupBy() method groups elements of an object according to
string values returned from a callback function. The Object.groupBy()
method does not change the original object