Learning fast development : ECMA script useful methods
Hi Friends,
In this blog post, I will be updating the most useful ECMA methods that are often used and are very useful in getting the expected results and also speeds up the development time.
Go through them and try them, and I am quite confident you'll see the difference.
Template Literals
xxxxxxxxxx
const Currency1 = "Dollars";
const Currency2 = "Rupees";
const exchange_rate = 72;
news_string = "Today 100 " + Currency1 + " are trading for " + exchange_rate * 100 + " " + Currency2;
//The normal way
console.log(news_string); //Today 100 Dollars are trading for 7200 Rupees
//The template literal way
news_string_tl = `Today 100 ${Currency1} are trading for ${exchange_rate * 100} ${Currency2}`;
console.log(news_string_tl); //Today 100 Dollars are trading for 7200 Rupees
Destructuring Objects
xxxxxxxxxx
const financialInfo = {
City: "New York",
Artform: "Jazz",
Artist: "Miles Davis",
TicketCurrency: "$",
Cost: 200,
};
//Destructuring Object
const { Artist, TicketCurrency, Cost } = financialInfo;
console.log(`For ${Artist} please pay ${Cost}${TicketCurrency}`); // For Miles Davis please pay 200$
//Object destructuring with alias creation
const { Artist: a, TicketCurrency: tc, Cost: c } = financialInfo;
console.log(`For ${a} please pay ${c}${tc}`); // For Miles Davis please pay 200$
For-of Loop
xxxxxxxxxx
let details = [11, 13, 17, "Prime Numbers"];
//Looping through array
for (const detail of details) {
console.log(detail);
}
//Output
11
13
17
Prime Numbers
Spread Operator
xxxxxxxxxx
array1 = [1, 2, 3, 5];
object1 = {
key1: "value1",
key2: "value2",
};
//Spread operator in array
array2 = [10, array1];
console.log(array2); // [10,1,2,3,5]
//Spread operator in object
object2 = {
key3: "value3",
object1,
};
console.log(object2); // {key3:'value3', key1:'value1', key2:'value2'}
Arrow Functions
x
//The old way of writing functions
var addf = function add(x, y) {
var sum = x + y;
return sum;
};
console.log(`The sum is : ${addf(10, 20)}`); //The sum is : 30
//The new way of writing functions
var newAdd = (x, y) => x + y;
console.log(`The sum is : ${newAdd(10, 20)}`); //The sum is : 30
Default Parameters
x
//Default parameter z
var sum = function (x, y, z = 10) {
return x + y + z;
};
console.log(`Sum = ${sum(10, 20)}`); //Sum = 40
console.log(`Sum = ${sum(10, 20, 5)}`); //Sum = 35
Sets
x
//Create set from an array
var set1 = new Set([1, 2, 2, 3, 3, 3, 4, 4, 4, 4]);
console.log(set1); //[1,2,3,4]
//Add a new element to it
set1.add(5);
console.log(set1); //[1,2,3,4,5]
Hope these methods will help you in gaining momentum while coding and getting the result right.
Happy coding..!!
#Javascript #SharePointWidgets #Typescript
Comments
Post a Comment