Skip to content

Instantly share code, notes, and snippets.

@pnchinmay
Forked from tushartiwari7/convertES6toES5.js
Created November 6, 2022 15:21
Show Gist options
  • Save pnchinmay/0331d6f8846d4942acff430c5fffd525 to your computer and use it in GitHub Desktop.
Save pnchinmay/0331d6f8846d4942acff430c5fffd525 to your computer and use it in GitHub Desktop.
ES6+ Assignment questions ft. @RohitDhatrak
// 1.
//ES5 syntax
var aloo = 1;
if (aloo == 1) {
var a = 2;
console.log(a);
}
console.log(aloo);
//ES6 equivalent
const aloo = 1;
if(aloo) {
const a=2;
console.log(a);
}
console.log(aloo);
// 2.
// Es5 syntax
var multiply = function(x, y) {
return x * y;
};
//ES6 syntax
const multiply = (x,y) => x*y;
// 3.
// Es5 syntax
var customer = {
name: "Bhaalo"
};
var card = {
amount: 20,
product: "Aaalo",
unitprice: 50
};
var message = "Hello " + customer.name + " wants to buy " + card.amount + " " + card.product +
" for price of " + card.unitprice + " per piece";
//ES6 syntax
const customer = {
name: "Bhaalo"
};
const card = {
amount: 20,
product: "Aaalo",
unitprice: 50
};
const message = `Hello ${customer.name} wants to buy ${card.amount} ${card.product} for price of ${card.unitprice} per piece`;
// 4.
// Es5 syntax
var Neog = ["Tanvi", "Swap", "Tanay", "MA", "CA", "PA", "TA"],
CEO = Neog[0],
Mentor = Neog[2];
//ES6 syntax
const [CEO,,Mentor] = ["Tanvi", "Swap", "Tanay", "MA", "CA", "PA", "TA"];
// 5.
// Es5 Syntax:
var arr = ["MA", "TA", "PA", "CA"];
var firstName = arr[0],
var surname = arr[1];
console.log(firstName);
console.log(surname);
// ES6 syntax:
const [firstName,surName] = ["MA","TA","PA","CA"];
console.log(`${firstName}
${surName}`);
// 6.
// ES5 syntax
var Aaloo = "Tasty";
var Bhaloo = "Cute";
var Obj = {
Aaloo: Aaloo,
Bhaloo: Bhaloo
};
//ES6 syntax:
let Aaloo="tasty", Bhaloo="Cute", Obj = {Aaloo,Bhaloo};
// 7.
// ES5 syntax
var a = 5;
var b = 10;
console.log("Fifteen is ".concat(a + b, " and n0t ").concat(2 * a + b, "."));
//ES6 syntax:
let a=5,b=10;
console.log(`Fifteen is ${a+b} and not ${2*a+b}.`);
// 8.
// ES5 syntax
var arithmeticsObj = {
sum: function sum(num1, num2) {
return num1 + num2;
},
multiply: function multiply(num1, num2) {
return num1 * num2;
}
};
//ES6 syntax:
const arithmeitic = {
sum: (num1,num2) => num1+num2,
multiply: (n1,n2) => n1*n2
}
// 9.
// ES5 syntax
var avengers = {
operation: "Assemble",
members: [
{
ironMan: "Tony Stark"
},
{
captainAmerica: "Steve Rogers"
},
{
blackWidow: "Natasha Romanoff"
}
]
};
var operation = avengers.operation,
members = avengers.members;
//ES6 syntax:
var avengers = {
operation: "Assemble",
members: [
{ ironMan: "Tony Stark" },
{ captainAmerica: "Steve Rogers" },
{ blackWidow: "Natasha Romanoff" }
]
};
[operation,members] = avengers;
//ES6 syntax:
const packIt = (...args) => console.log(args)
packIt(1,2,3,5,5);
// ES5 syntax
var packIt = function(n1,n2,n3,n4,n5) {
return console.log(n1,n2,n3,n4,n5);
}
// 1.
const hello = () => "Hello"
const welcome = () => "Welcome"
const [Hello = hello(), Welcome = welcome()] = ["Namaste"]
console.log(Hello, Welcome);
// ans
Namaste Welcome
// 2.
const obj = {
aloo : 1,
bhallo : 2
}
const {c : aloo = [2,3,4].push(5), aloo} = obj;
console.log(aloo);
//ans
error on line 15 because when we extract c from obj it was renamed as aloo
and has default value of 4 ([2,3,4].push(5) returns the updated length of the array)
And redeclaring a const variable again will cause an error.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment