JavaScript ES6 Q&A: Complex Problems Simple Solutions
3 min readJan 30, 2021
--
Javascript is one of the hottest programming languages of all time. For beginners or even mid-level programmers, this is a simple guide to build logic with most common but powerful functions of JavaScript ES6.
let foo = 1; // foo = true; // foo = 'a'; // foo = "Hello";
How to determine the data type of a variable?
return typeof(foo);
Alternatively, comparing datatypes:
return typeof(foo) === 'boolean';// This will return true if value of foo is boolean else will return false.
How to remove duplicates from an array?
let data = [“apple”, “orange”, “apple”, “banana”, “banana”, “apple”];distinct_array = […new Set(data)];
// distinct_array = ["apple", "orange", "banana"]
Alternative Method:
distinct_array = data.filter((item,pos,self) => self.indexOf(item) === pos);
How to check if an item is present in the array?
let data = [“blue”, “pink”, “red”, “green”];
return data.indexOf(“yellow”) ≤ -1
// will return true as yellow is not present in the array
— — — Array of objects — — —
let planets = [
{"name": "mercury", "position": 1, "colour": "grey"},
{"name": "venus", "position": 2, "colour": "Brown…