JavaScript Shorthands

Table Of Contents

  1. parseInt()
  2. Math.floor()
  3. String to Boolean
  4. If-Else statement
  5. XOR gate?
  6. string.split('')

#parseInt()

let x = "11";
console.log(parseInt(x)); // 100

parseInt is a function in JavaScript which is used to convert a string to a number. There is an awesome shorthand is available for parseInt. “+” is the shorthand. It’s very easy when writing a 1000-line-JS.

let y = "100";
console.log(+y); // 100

It works with parseFloat() too.

#Math.floor()

let z = 14.45;
Math.floor(z); // 14

Math.floor is a function that is used to return the largest integer value that is less than or equal to a number. “~~” is the shorthand for this function. I hope you have a smile now.

console.log(~~z); // returns 14

#String to Boolean

let strValue = "true";
strValue == "true" ? true : false; // true

I am sure you will think that this is the shorthand. But there is a even short way for this too. The operator used above is called a Ternary operator. You can use this as a shorthand for an If-Else loop. The below code is a shorthand for the string to boolean conversion. It also works for number values too.

let boolValue = !!strValue; // true

#If-Else statement

As mentioned earlier, you can use the ternary operator as shorthand for the If-Else statement-based assignment. But when using multiline-blocks in If-Else statement, the ternary operator wants to be extended as shown below.

age > 13 ? (
// age is higher than 13
) : (
// age is not higher than 13
)

#XOR gate?

We all know AND-OR gates. We use them in many places. Some of them may have heard the XOR gate. It is a mixture of AND-OR concepts. Here is the truth table for the XOR gate. for more.

ABA XOR B
000
011
101
110

The XOR gate returns true if the number of inputs is odd. Sometimes we want this too. AND and OR can be accessed in JS with && and || operators respectively. But how we can access XOR?

let A = true;
let B = false;

// these will work as XOR.

// 1
(!A && B) ||
    ((A && !B)(
        // 2
        !A || !B
    ) &&
        (A || B));

Both of the above expressions work as a XOR gate. There is an operator for XOR: “^”. We don't have to write the XOR expression using && and ||.

Boolean(A ^ B); // ^ works as XOR

The above short-handed code will work. The there is a small problem with the operator, it returns a number instead of a boolean.

Note that the operator is BITWISE XOR. Which means it will return other values when the operands are numbers or other types.

#string.split('')

Sometimes, we want to split a string into one-letter-array as shown below.

let name = "Sahithyan";
let letters = name.split(""); // [S, a, h, i, t, h, y, a, n]

In most cases, it’s ok to write .split(‘’) one or two times in our code. But when we want to split a string into a letter array, this gets horrible. But, fortunately, there is a shorthand for the split method.

let name = "Sahithyan";
let letters = [...name]; // [S, a, h, i, t, h, y, a, n]

Unfortunately, I don’t know what happens there. if you know, please tell that in the comments. But I am sure that it works.