Mastering JavaScript Fundamentals: Day 4 Interview Prep Guide
Written on
Day 4: A Journey Through JavaScript Fundamentals
Welcome to Day 4 of our 100-day JavaScript interview preparation series! This extensive guide aims to equip you for your upcoming JavaScript interview. Join us as we document this enlightening journey filled with learning opportunities, practical exercises, and personal growth.
Practical Questions for Day 4
Question 1: What is the output of typeof null in JavaScript?
The outcome will be "object." This peculiar behavior stems from a historical bug in JavaScript, which remains due to backward compatibility.
Question 2: How can you verify if a variable is an array without using typeof?
You can utilize the Array.isArray() method, as shown below:
let myarr = [1, 2, 3];
console.log(Array.isArray(myarr));
Question 3: Describe the distinction between the == and === operators in JavaScript.
The == operator performs a comparison after converting both operands to a common type, whereas === (strict equality) checks both the value and type without any type conversion.
Examples:
- 5 == "5"; // true (string "5" is coerced to number 5)
- 0 == false; // true (false is coerced to number 0)
- null == undefined; // true (equal in value, different types)
With ===, however:
- 5 === "5"; // false (types differ)
- 0 === false; // false (types differ)
- null === undefined; // false (different types)
Question 4: What does console.log(+'10') output?
The result will be 10, as the unary plus operator converts its operand into a number.
Question 5: How can a string be transformed into a number in JavaScript?
You can utilize either parseInt() for integers or parseFloat() for floating-point numbers. For instance:
let str = "123";
let num = parseInt(str);
console.log(num); // Output: 123
Question 6: What methods can be used to convert a string to a number?
You can apply the Number() function, the unary plus operator, or parseFloat() or parseInt() with the unary plus for numeric strings. Here’s an example using the unary plus:
let str = "42";
let num = +str;
console.log(num); // Output: 42
Question 7: What does 0 == false evaluate to in JavaScript?
This expression evaluates to true, as 0 is considered falsy, making it loosely equal to false.
Question 8: How can you determine if a variable is defined in JavaScript?
There are various methods to check if a variable is defined:
- Using `typeof`:
let x;
if (typeof x !== 'undefined') {
console.log('x is defined');
} else {
console.log('x is not defined');
}
- Using strict equality:
let y;
if (y !== undefined) {
console.log('y is defined');
} else {
console.log('y is not defined');
}
- Using the `in` operator:
if ('z' in window) {
console.log('z is defined');
} else {
console.log('z is not defined');
}
- Using a try-catch block:
try {
let a;
let b = a; // This will throw a ReferenceError if 'a' is not defined
console.log('a is defined');
} catch (error) {
console.log('a is not defined');
}
Question 9: Create a function to check if a number is even or odd without the modulus operator.
function isEven(num) {
return (num / 2) === Math.floor(num / 2);
}
Question 10: Explain truthy and falsy values in JavaScript.
In JavaScript, values can be evaluated as true or false in Boolean contexts, such as conditions.
Truthy Values: These evaluate to true:
- Non-empty strings ("hello", "true", "0")
- Non-zero numbers
- Non-empty objects
Falsy Values: These evaluate to false:
- The empty string ""
- The number 0
- null, undefined, NaN, and the boolean value false
let x = "hello";
if (x) {
console.log("x is truthy");
} else {
console.log("x is falsy");
}
let y = 0;
if (y) {
console.log("y is truthy");
} else {
console.log("y is falsy");
}
Question 11: What will be the output of console.log(1 + '1' - 1)?
The output will be 10, as the concatenation of '1' and 1 yields the string '11', which is then converted back to a number in the arithmetic operation.
Remember to share knowledge generously and treat others with kindness! If you find these videos beneficial, don't forget to subscribe, give claps, and follow for more support.
Cheers,
Kirti Kaushal