0% found this document useful (0 votes)
4 views

What will be the output of the following code

The document contains a series of JavaScript code snippets that demonstrate various concepts related to truthy and falsy values, type coercion, and conditional statements. Each code snippet is followed by an explanation of its output, highlighting the behavior of JavaScript in different scenarios. The examples cover a range of topics including undefined variables, null values, strict equality, and the use of logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

What will be the output of the following code

The document contains a series of JavaScript code snippets that demonstrate various concepts related to truthy and falsy values, type coercion, and conditional statements. Each code snippet is followed by an explanation of its output, highlighting the behavior of JavaScript in different scenarios. The examples cover a range of topics including undefined variables, null values, strict equality, and the use of logical operators.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

// What will be the output of the following code?

if (0) {

console.log("This will print");

} else {

console.log("This will not print");

Answer:

"This will not print". 0 is a falsy value, so the else block executes.

Question 2: Implicit Coercion

js

Copy code

// What will be the output of the following code?

if ("false") {

console.log("Truthy");

} else {

console.log("Falsy");

Answer:

"Truthy". The string "false" is a non-empty string, which is truthy.

Question 3: undefined in if Statement

js

Copy code

// What will be the output of the following code?

let x;

if (x) {

console.log("Defined");

} else {
console.log("Undefined");

Answer:

"Undefined". An uninitialized variable is undefined, which is falsy.

Question 4: null in if Statement

js

Copy code

// What will be the output of the following code?

let y = null;

if (y) {

console.log("Not null");

} else {

console.log("Null");

Answer:

"Null". null is a falsy value.

Question 5: Comparing Different Types

js

Copy code

// What will be the output of the following code?

if (5 == "5") {

console.log("Equal");

} else {

console.log("Not equal");

Answer:
"Equal". The == operator performs type coercion, so 5 and "5" are considered equal.

Question 6: Strict Equality Comparison

js

Copy code

// What will be the output of the following code?

if (5 === "5") {

console.log("Strictly equal");

} else {

console.log("Not strictly equal");

Answer:

"Not strictly equal". The === operator does not perform type coercion, so 5 and "5" are not
considered equal.

Question 7: if with NaN

js

Copy code

// What will be the output of the following code?

let z = NaN;

if (z) {

console.log("Not NaN");

} else {

console.log("NaN");

Answer:

"NaN". NaN is a falsy value.

Question 8: Short-Circuit Evaluation


js

Copy code

// What will be the output of the following code?

if (false || true) {

console.log("One of them is true");

Answer:

"One of them is true". The || operator short-circuits and returns true when at least one operand is
truthy.

Question 9: if with Logical AND

js

Copy code

// What will be the output of the following code?

if (true && false) {

console.log("Both are true");

} else {

console.log("At least one is false");

Answer:

"At least one is false". The && operator returns false when any operand is falsy.

Question 10: if with Empty Array

js

Copy code

// What will be the output of the following code?

if ([]) {

console.log("Empty array is truthy");

} else {
console.log("Empty array is falsy");

Answer:

"Empty array is truthy". An empty array is truthy in JavaScript.

Question 11: if with Empty Object

js

Copy code

// What will be the output of the following code?

if ({}) {

console.log("Empty object is truthy");

} else {

console.log("Empty object is falsy");

Answer:

"Empty object is truthy". An empty object is truthy in JavaScript.

Question 12: Double Negation

js

Copy code

// What will be the output of the following code?

let value = !!"";

if (value) {

console.log("Truthy");

} else {

console.log("Falsy");

Answer:
"Falsy". The double negation (!!) converts a value to a boolean. An empty string is falsy.

Question 13: if with Multiple Conditions

js

Copy code

// What will be the output of the following code?

if (1 && 0 || 2) {

console.log("Condition met");

} else {

console.log("Condition not met");

Answer:

"Condition met". The expression evaluates as (false || true), which is true.

Question 14: if with Function Return Value

js

Copy code

// What will be the output of the following code?

function isPositive(num) {

return num > 0;

if (isPositive(-1)) {

console.log("Positive");

} else {

console.log("Not positive");

Answer:

"Not positive". The function returns false because -1 is not greater than 0.

Question 15: if with typeof


js

Copy code

// What will be the output of the following code?

if (typeof undefinedVariable === "undefined") {

console.log("Undefined variable");

} else {

console.log("Defined variable");

Answer:

"Undefined variable". typeof returns "undefined" for undeclared variables.

Question 16: if with Inverted Condition

js

Copy code

// What will be the output of the following code?

let flag = true;

if (!flag) {

console.log("Flag is false");

} else {

console.log("Flag is true");

Answer:

"Flag is true". The ! operator negates the boolean value.

Question 17: if with Nullish Coalescing

js

Copy code

// What will be the output of the following code?


let name = null;

if (name ?? "Unknown") {

console.log("Name is set");

} else {

console.log("Name is not set");

Answer:

"Name is set". The ?? operator returns "Unknown", which is truthy.

Question 18: if with Function and Empty Return

js

Copy code

// What will be the output of the following code?

function doNothing() {}

if (doNothing()) {

console.log("Something was returned");

} else {

console.log("Nothing was returned");

Answer:

"Nothing was returned". The function returns undefined, which is falsy.

Question 19: if with an Immediately Invoked Function Expression (IIFE)

js

Copy code

// What will be the output of the following code?

if ((function() { return 2; })()) {

console.log("IIFE returns a truthy value");

} else {
console.log("IIFE returns a falsy value");

Answer:

"IIFE returns a truthy value". The IIFE returns 2, which is truthy.

Question 20: if with an Assignment in Condition

js

Copy code

// What will be the output of the following code?

let a = 0;

if (a = 1) {

console.log("Assignment returns a truthy value");

} else {

console.log("Assignment returns a falsy value");

You might also like