#1, !!
Converts Object to boolean. If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.
!oObject // inverted boolean
!!oObject // non inverted boolean so true boolean representation
So !! is not an operator, it's just the ! operator twice.
#2, ??
JavaScript nullish coalescing operator.
It returns the right operand (rightExpression) if the left operand (leftExpression) is null or undefined.
let count;
let result = count || 1 ;
console.log(result); // 1let count = 0;
let result = count || 1;
console.log(result); // 0
#3, style your console.log
console.log("%cThis is a green text. %cThis is red text and bold", "color:green", "color:red; font-weight:bold")
#4. short-hand to check and use variable
console.log(user?.user_info?.emailAddress)
or
if(user && user.user_info) {
console.log(user.user_info.emailAddress)
}
it also works for function or array as well
yourObj.maybeFunctionName?.( )
yourObj.maybeArrayName?.[0]
your function will be called if it is defined and exist.
#5. object short-hand for name and value. When your key and value are the same variable name, you don't have to repeat it.
const color = "whatever"
const size = "whatever size is"
var Car = {
color,
size,
notSameName: color
}var Cart = {
color: color,
size: size,
notSameName: color
}
#6. you should always load the javascript at the head instead of loading in the bottom of the page. To do that, you use the keyword 'defer'