10 Most Common JavaScript FAQs in One Place

10 Most Common JavaScript FAQs in One Place
Photo by Blake Connally / Unsplash

1) What is the use of === in JavaScript?

The triple equals operator (===) is a comparison operator in JavaScript that returns true if the two operands are strictly equal (type and value) and false otherwise.

Strict equality (===) is different from abstract equality (==). Abstract equality allows for implicit type coercion, while strict equality does not.

For example, the following expressions are all true:

1 == true
2 == "2"

However, the following expressions are all false:

1 === true
2 === "2"

The reason the second set of expressions are all false is because, with strict equality, the operands must have the same type. In the first set of expressions, the == operator is doing type coercion, which means it is implicitly converting the operands to the same type before doing the comparison.

In general, it is best to always use the === operator, unless you have a specific reason to use the == operator.

2) What is the difference between == and === in JavaScript?

In JavaScript, both == (double equals) and === (triple equals) are comparison operators. They both compare two values and return a boolean (true or false) value indicating whether the comparison is true.

The == operator will perform a type conversion if necessary, whereas the === operator will not. This means that == is more likely to return true when comparing two values of different types, whereas === is more likely to return false.

For example, == will return true when comparing the number 1 with the string "1", but === will return false.

When comparing two values, it is generally advisable to use the === operator, as it is more likely to give the expected results.

3) What is the difference between null and undefined in JavaScript?

In JavaScript, null is a value that represents "no value" or "nothing". It is a primitive data type and can be assigned to a variable as a representation of no value.

Undefined, on the other hand, is a value that represents "no value" or "nothing" as well, but it cannot be assigned to a variable. When a variable is declared but not given a value, it is automatically assigned the value of undefined.

Null and undefined both represent the absence of a value, but they are two different values.

Null is a value that can be assigned to a variable, while undefined cannot.

Null represents "no value", while undefined represents "no value" or "nothing".

Null is an empty value, while undefined is not.

4) What is the use of isNaN in JavaScript?

isNaN() is a built-in function in JavaScript that determines whether a value is NaN (Not-A-Number).

This function is useful for handling values that could be any type, including strings. If a string is passed to isNaN(), it will first be converted to a number and then checked for NaN-ness.

For example, the following values are all considered to be NaN:

"abc" //string
true //boolean

null //null

{} //object

[] //array

undefined //undefined

In addition, any value that cannot be parsed into a number is also considered to be NaN. For example:

"1a" //string
" " //string
" \\t\\r\\n" //string
NaN //number

The isNaN() function is often used to check if a value is a valid number before performing any mathematical operations on it. This is because, as we saw above, any operation on a NaN value will result in a NaN return value.

For example, consider the following code:

var x = "5";
var y = 2;
var z = x * y;
console.log(z); //prints 10

In this code, we're multiplying a string (x) by a number (y). Because JavaScript is loosely typed, it automatically converts the string to a number before performing the multiplication.

However, if we try to multiply a string by a non-numeric value, the result will be NaN:

var x = "5";
var y = "a";
var z = x * y;
console.log(z); //prints NaN

In this case, isNaN() can be used to check if the value of y is a valid number before performing the multiplication:

var x = "5";
var y = "a";
if(!isNaN(y)){
var z = x * y;
console.log(z); //prints NaN
}
else{
console.log("y is not a number!");
}

This code will print "y is not a number!" because the isNaN() function returns true when the value of y is not a number.

5) What is the use of typeof in JavaScript?

The typeof operator is used to get the data type (returns a string) of its operand.

Syntax
typeof operand
Parameters
operand
The operand whose type is to be returned. Can be of any type.

Description

The typeof operator returns a string indicating the type of the unevaluated operand.

The following are the possible values that typeof returns:

"undefined" - if the operand is not defined
"boolean" - if the operand is a Boolean value
"string" - if the operand is a string
"number" - if the operand is a number
"object" - if the operand is an object
"function" - if the operand is a function

If the operand is an array, then typeof returns "object".
If the operand is null, then typeof returns "object".
If the operand is a symbol, then typeof returns "symbol".
If the operand is a bigint, then typeof returns "bigint".
**Note**:
typeof null is "object". This is a bug in JavaScript.

Examples:

typeof 3.14;             // "number"
typeof "hello";          // "string"
typeof true;             // "boolean"
typeof undefined;        // "undefined"
typeof Symbol("foo");    // "symbol"
typeof {};               // "object"
typeof [];               // "object"
typeof function(){};    // "function"
typeof typeof 3.14;      // "string" (typeof always returns a string)

In the following example, typeof is used inside a user-defined function to check the data type of its arguments. If the argument is a number, typeof returns "number", if the argument is a string, typeof returns "string", and so on.

function checkType(value) {
if (typeof value === "string") {
console.log("value is a string");
} else if (typeof value === "number") {
console.log("value is a number");
} else if (typeof value === "boolean") {
console.log("value is a boolean");
} else if (typeof value === "undefined") {
console.log("value is undefined");
} else if (typeof value === "object") {
console.log("value is an object");
} else if (typeof value === "function") {
console.log("value is a function");
} else if (typeof value === "symbol") {
console.log("value is a symbol");
} else if (typeof value === "bigint") {
console.log("value is a bigint");
}
}
checkType(3.14);       // "value is a number"
checkType("hello");    // "value is a string"
checkType(true);       // "value is a boolean"
checkType(undefined);  // "value is undefined"
checkType({});         // "value is an object"
checkType([]);         // "value is an object"
checkType(function(){}); // "value is a function"
checkType(Symbol("foo")); // "value is a symbol"
checkType(3n);         // "value is a bigint"

6) What is the use of parseInt in JavaScript?

JavaScript's parseInt function parses a string and returns an integer. The function takes two arguments: the string to parse and an optional radix (base) argument. The radix argument specifies the base of the number in the string. It can be an integer between 2 and 36. The default value is 10.

If the radix argument is omitted, JavaScript assumes the following:

If the string begins with "0x", the radix is 16 (hexadecimal)
If the string begins with "0", the radix is 8 (octal). This feature is deprecated
If the string begins with any other value, the radix is 10 (decimal)

If the first character can't be converted to a number, parseInt returns NaN.

Here are some examples:

parseInt("10") // returns 10
parseInt("10", 10) // also returns 10
parseInt("10", 16) // returns 16
parseInt("0x10") // also returns 16
parseInt("10", 8) // returns 8
parseInt("10", 2) // returns 2
parseInt("10", 36) // returns 36

7) What is the use of parseFloat in JavaScript?

parseFloat is a built-in function in JavaScript that is used to parse a string and return a floating point number.

If the string cannot be parsed as a valid floating point number, then the function will return NaN (Not a Number).

parseFloat is often used when reading data from a string, such as when reading a form field value that has been entered by a user.

Example:

var x = parseFloat("10.5"); // x will be equal to 10.5

parseFloat can also be used to convert a string to an integer, by simply passing in the radix (base) as the second argument.

Example:

var x = parseFloat("10", 10); // x will be equal to 10

8) What is the use of escape in JavaScript?

In JavaScript, the escape function is used to encode a string. This function encodes special characters, such as carriage return, line feed, tab, backslash, and single and double quotes. The encoded string is returned as a new string.

The escape function is useful for ensuring that a string does not contain any characters that could potentially cause problems when used in a URL or other context. For example, if a string contains a space character, it will be encoded as a plus character (+). This can be useful when constructing a query string for a URL, as spaces are not typically allowed in URLs.

The escape function can also be used to encode non-ASCII characters, such as characters from other languages. This can be useful when working with internationalized text.

The escape function is part of the ECMAScript standard, and is available in most JavaScript implementations.

9) What is the use of unescape in JavaScript?

The unescape function computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. For example, the string "Hello%20World" will be converted to "Hello World".

This function is deprecated. Use the decodeURI or decodeURIComponent instead.

10) What is the use of encodeURI in JavaScript?

In JavaScript, encodeURI is a function that is used to encode a URI. This function encodes a URI by replacing certain characters with percent-encoded characters. These percent-encoded characters are then used in the URI in place of the original characters.

The encodeURI function is useful when you want to encode a URI for use in a query string or in a path segment of a URI. For example, if you want to include a space in a query string, you would need to encode the space as %20 .

The encodeURI function is also useful for encodingURIs for use in HTML. When a URI is used in an HTML document, certain characters have special meaning. For example, the character # has special meaning in HTML. If you want to use the character # in a URI, you would need to encode it as %23 .

The encodeURI function is not intended for use in decoding URIs. For decoding URIs, you should use the decodeURI function.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe