String and Integer Operations
console.log(2 + '2') // 22
console.log(2 - '2') // 0
/*
+ is a number or string operator. If both operands are number, it adds. Otherwise it concatenates.
- is a number operator.
*/
Three comparative operators in a row
console.log(5 < 6 <7) // true
console.log(7 > 6 >5) // false
/*
5 < 6 < 7 ==(5<6=true)==> true < 7 ==(1<7)==> true
7 > 6 > 5 ==(7>6=true)==> true > 5 ==(1>5)==> false
*/
Arguments in arrow functions
let a = function() { return arguments; }
let b = () => { return arguments; }
console.log(a("hi")) // "hi"
console.log(b("hi")) //
/* "arguments" doesn't bind with arrow function. Alternatively we can use triple-dot parameters in arrow function. */
Object.freeze() and Object.Seal()
let p1 = {"name": "abc"};
Object.freeze(p1);
p1.age = 10;
console.log(p1); // { name: 'abc' }
p1.name = "bcd";
console.log(p1); // { name: 'abc' }
let p2 = {"name": "abc"};
Object.seal(p2);
p2.age = 10;
console.log(p2); // { name: 'abc' }
p2.name = "bcd";
console.log(p2); // { name: 'bcd' }
/*
Object.freeze(person) restricts from updating an object
Object.freeze(person) restricts from adding a new property to an object
*/
Math.max()
console.log(Math.max()) // -Infinity
/* lowest value so that it can get the max value after comparison */
Adding 2 empty arrays
console.log([] + []) // empty string
/*
+ applies to either number or string. If neither, it typecases to string.
String([]) = String({}) = empty string
*/
Tagged Templates
function a() {
return 'hello'
}
const b = a 'hi'
console.log(b); // hello
/* a 'hi' = a('hi') */
This keyword
function y() {
console.log(this.length)
}
var x = {
length: 5,
method: function(y) {
arguments[0]();
}
};
x.method(y, 1); // 2
/*
x's method gets 2 arguments: { [Function: y], 1 }
so, this for function y is { [Function: y], 1 }
Hence this.length = 2
*/
StringConstructor
var x = "constructor"
console.log(x[x](01)) // "01"
/*
x is a string
x[x] = x["constructor"] = x.constructor
constructor of any string is a String function
passing any value to String function gets the value in String
*/
Floating point addition
console.log(0.1 + 0.2); // 0.30000000000000004
/*
as decimals are in base 10, but computer only understands base 2; this bug is encountered
*/
Array with Spread Operator
const a = [1, 2, 3, 4, 5]
console.log(...arr) // 1 2 3 4 5
Function with Rest Operator
function getAge(...args) {
console.log(typeof args); # object
}
getAge(21);
Function inside Object
const user = {
email: "default_email@mail.com",
updateEmail: email => {
this.email = email;
}
}
user.updateEmail("updated_email@mail.com");
console.log(user.email); // default_email@mail.com
Type of typeof
let num = 1;
console.log(typeof typeof num); // string