16. Singly or Doubly Invoked Function
function getTotal() 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 = ;
18. Order Logged Out
function logNumbers() 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 = ; // 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 () ; // constructor function function Car(color, brand, year) Car.prototype.getColor = function () ; 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 = ; const roadTrip2 = ; 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 = ; const user2 = ; 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([, ].indexOf()); // -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 = ; // both variables are referencing same objects var person2 = person1; person2.name = 'Kyle'; //
27. Strings and Arrays
const data1 = 'Jordan Smith'; const data2 = [].filter.call(data1, function(elem, index) ); // 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 = ; const 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() y(); console.log(x); // 10
30. Withdraw From Account
const account1 = ; const account2 = ; const withdrawFromAccount = function (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