#100DAYSOFCODE Day73

#100DAYSOFCODE Day73

Learn about ES Next

ES Next

JavaScript Nullish Coalescing Operator

const name = null ?? null
console.log(name) //null

const age = undefined ?? 28
console.log(age) //2/

let count
let result = count ?? 1
console.log(result) // undefined

const result2 = null || undefined
console.log(result2) // undefined

The Logical AND operator

If a can be converted to true, the && operator returns the b; otherwise, it returns the a. In fact, this rule is applied to boolean values.

a = true
b = 'b'
let result = a && b; // 'b'
a = false
b = 'b'
let result = a && b; // false

JavaScript Optional Chaining Operator

without optional:
let user = getUser(2);
let profile = (user !== null || user !== undefined)
            ? user.profile
            : undefined;
with optional:
let user = getUser(-1);
let avatar = user?. profile?. avatar;
let profile = user?. profile ?? "unknown"; //if user is false return 'unknown';

Using optional chaining operator with function calls:

let file = {
    read() {
        return 'file content';
    },
}
let compressedData = file.compress(); // CRASH
let compressedData = file.compress?.(); // undefined

function getUser(id, callback) {
    let user = {
        id: id,
        username: 'admin'
    };
    // test if the callback exists
    callback ?. (user);
    return user;
}

JavaScript BigInt:

let x = Number.MAX_SAFE_INTEGER;
x = x + 1; // 9007199254740992
x = x + 1; // 9007199254740992 (same as above)

let x = BigInt(Number.MAX_SAFE_INTEGER);
x = x + 1; // 9007199254740992n
x = x + 1; // 9007199254740993n (correct now)
console.log(typeof bigInt); // bigint