16. Singly or Doubly Invoked Function
function getTotal() { //convert object to an array var args = Array.prototype.slice.call(arguments); if (args.length === 2) { return args[0] + args[1]; } else if (args.length === 1) { return function(num2) { return args[0] + num2; }; } } console.log(getTotal(10, 20)); console.log(getTotal(5, 40)); console.log(getTotal(3)(30)); console.log(getTotal(8)(12));
Output:
30 45 33 20 Process finished with exit code 0
17. JSON Data
const myJsonObj = { "myString": "hello world", "myNumber": 12345.6789, "myNull": null, "myBoolean": true, "myArray": [20, 30, "orange"], "myObject": { "name": "Sam", "age": 30 } };
18. Order Logged Out
function logNumbers() { console.log(1); //added in event loop setTimeout(function(){console.log(2)}, 1000); //added in event loop setTimeout(function(){console.log(3)}, 0); console.log(4); } logNumbers();
Output:
1 4 3 2 Process finished with exit code 0
19. Making an Object
Three different ways to Create an Object.
// object literal syntax const myBoat = { length: 24, maxSpeed: 45, passengers: 14, getLength: function () { return this.length; } }; // new keyword & Object constructor const student = new Object(); // const student = {}; student.grade = 12; student.gradePointAverage = 3.7; student.classes = ["English", "Algebra", "Chemistry"]; student.getClasses = function () { return this.classes; }; // constructor function function Car(color, brand, year) { this.color = color; this.brand = brand; this.year = year; } Car.prototype.getColor = function () { return this.color; }; const carlysCar = new Car('blue', 'ferarri', 2015); const jimsCar = new Car('red', 'tesla', 2014); console.log(carlysCar.getColor()); console.log(jimsCar.getColor());
20. Type of Data Types
console.log(typeof null); // object console.log(typeof undefined); // undefined console.log(typeof {}); // object console.log(typeof []); // object console.log(Array.isArray([])); // true console.log([] instanceof Array); // true
21. Bind Method
this.distance = 10000; const roadTrip1 = { distance: 3000, getDistance: function (unit, caption) { return this.distance + unit + caption; } }; const roadTrip2 = { distance: 5000 }; const getTripDistance = roadTrip1.getDistance.bind(roadTrip2, 'km',' in total'); console.log(getTripDistance()) // Output: // 5000km in total // Process finished with exit code 0
22. Two Objects
Two variables: user1, user2 are referencing different objects in memory ( Pass by Reference (objects) ). Convert object into string to compare them.
The
JSON.stringify()
method converts a JavaScript object or value to a JSON string.
const user1 = { name: 'Jordan', age: 28, }; const user2 = { name: 'Jordan', age: 28, }; console.log(user1 == user2); console.log([] == []); //array is object console.log(JSON.stringify(user1) === JSON.stringify(user2)); // Output: // false // false // true // Process finished with exit code 0
23. Array Constructor
var arr1 = []; var arr2 = new Array(50); //initial an empty array with 50 length var arr3 = new Array(1, 2, "three", 4, "five"); var arr4 = new Array([1, 2, 3, 4, 5]); var arr5 = new Array('Hello world'); console.log('arr1: ', arr1); console.log('arr2: ', arr2); console.log('arr3: ', arr3); console.log('arr4: ', arr4); console.log('arr5: ', arr5);
Output:
arr1: [] arr2: [ <50 empty items> ] arr3: [ 1, 2, 'three', 4, 'five' ] arr4: [ [ 1, 2, 3, 4, 5 ] ] arr5: [ 'Hello world' ] Process finished with exit code 0
24. Array IndexOf
const myArray = [5]; const anotherArray = myArray; console.log([10, 20, 30, 40, 50].indexOf(30)); // 2 console.log([{ name: 'Pam' }, { name: 'Kent' }].indexOf({ name: 'Kent' })); // -1 objects are passed by reference console.log('hello world'.indexOf('o')); // 4 console.log([[1], [2], [3], [4]].indexOf([2])); // -1 array is passed by reference console.log([[1], [2], [3], [4], myArray].indexOf(anotherArray)); //4
25. Equivalent Numbers
toFixed()
return string type.
console.log(900.9 === 300.3 * 3); console.log((300.3 * 3)); // fixed to two decimal, then convert string to Number console.log(Number((300.3 * 3).toFixed(2))); // get total 12 digits console.log(Number((300.3 * 3).toPrecision(12))); console.log(((300.3 * 10) * 3) / 10); // Output: // false // 900.9000000000001 // 900.9 // 900.9 // 900.9
26. Objects and Strings
var string1 = 'Tampa'; var string2 = string1; string1 = 'Venice'; console.log(string2); // Tampa var person1 = { name: 'Alex', age: 30 }; // both variables are referencing same objects var person2 = person1; person2.name = 'Kyle'; // { name: 'Kyle', age: 30 }
27. Strings and Arrays
const data1 = 'Jordan Smith'; const data2 = [].filter.call(data1, function(elem, index) { return index > 6; }); // only use 'read-only' methods: filter, forEach, map, some, every, etc. on string // cannot use: push, pop, splice, shift, reverse, etc. console.log(data2);
28. Object Properties
const a = {}; const b = { name: 'b' }; const c = { name: 'c' }; a[b] = 200; // convert b object to a string // a['[object Object]'] = 200 a[c] = 400; // set again a['[object Object]'] = 400 console.log(a[b]); // console.log(a['[object Object]']);
29. X and Y
var x = 10; function y() { // x() hoisted here // function x() { // } //resign x() to 100 x = 100; return; function x() {} } y(); console.log(x); // 10
30. Withdraw From Account
const account1 = { name: 'Jen', totalAmount: 5000, deductAmount: function (amount) { this.totalAmount -= amount; return 'Amount in account: ' + this.totalAmount; }, }; const account2 = { name: 'James', totalAmount: 8000, }; const withdrawFromAccount = function (amount) { return account1.deductAmount.bind(account2, amount); }; console.log(withdrawFromAccount(500)()); console.log(withdrawFromAccount(200)()); // Amount in account: 7500 // Amount in account: 7300 // Process finished with exit code 0
Leave A Comment