DEV Community

Cover image for JavaScript week-1 Mock Questions with Answers
Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

JavaScript week-1 Mock Questions with Answers

1️⃣ Tell me about yourself.

💡 Tip: Prepare a 1–2 minute introduction covering:
• Name
• Education
• Skills/Technologies
• Projects
• Experience (if any)
• Career Goal


2️⃣ Display: none vs Visibility: hidden

Both properties are used to hide elements, but they behave differently.

display: none

  • Completely removes the element from the page.
  • The element is not visible.
  • The element does not occupy any space.
  • Other elements move up and take its place.

Example:

display: none

Before:
A B C

After hiding B:
A C

visibility: hidden

  • Hides the element from view.
  • The element still occupies space.
  • Other elements do not move.

Example:

visibility: hidden

Before:
A B C

After hiding B:
A _ C

Quick Difference

display:none visibility:hidden
Removes element completely Only hides element
No space occupied Space remains
Layout changes Layout remains same

3️⃣ px vs % vs em vs rem

These are commonly used CSS units for sizing.

px (Pixels)

A fixed unit of measurement.

Example:

font-size: 16px;

  • Always remains 16 pixels.
  • Not affected by parent element size.

% (Percentage)

Relative to the parent element.

Example:

width: 50%;

If parent width is 1000px:

Result = 500px

em

Relative to the font size of the parent element.

Example:

Parent font-size = 16px

font-size: 2em;

Result:

32px

rem

Relative to the root (html) font size.

Example:

html {
font-size: 16px;
}

font-size: 2rem;

Result:

32px

Quick Summary

  • px → Fixed size
  • % → Relative to parent
  • em → Relative to parent font size
  • rem → Relative to root font size

Interview Tip

For responsive websites, rem is generally preferred because it provides consistent sizing across the application.


4️⃣ Optional Chaining (?.) Operator

Optional Chaining allows us to safely access nested object properties without throwing an error if a property doesn't exist.

Without Optional Chaining

const user = {
name: "John"
};

console.log(user.address.city);

Output:

Error: Cannot read properties of undefined

Reason:
address does not exist, so JavaScript throws an error.

With Optional Chaining

const user = {
name: "John"
};

console.log(user?.address?.city);

Output:

undefined

Instead of throwing an error, JavaScript safely returns undefined.

Benefits

  • Prevents application crashes.
  • Makes code cleaner.
  • Reduces multiple if conditions.

5️⃣ Bitwise Operators in JavaScript

Bitwise operators work directly on binary numbers (0 and 1).

AND (&)

Returns 1 only when both bits are 1.

Example:

5 & 1

Binary:

5 = 101
1 = 001

Result:

001 = 1

Output:

1

OR (|)

Returns 1 if either bit is 1.

Example:

5 | 1

Binary:

101
001

Result:

101 = 5

Output:

5

XOR (^)

Returns 1 only when bits are different.

Example:

5 ^ 1

Binary:

101
001

Result:

100 = 4

Output:

4

NOT (~)

Flips all bits.

Example:

~5

Output:

-6

Common Bitwise Operators

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT)
  • << (Left Shift)
  • > > (Right Shift)

Interview Tip

Bitwise operators are commonly used in:

  • Flags and permissions
  • Low-level programming
  • Performance optimizations

6️⃣ Type Casting

Type Casting means converting one data type into another.

Implicit Type Casting

JavaScript automatically converts the type.

Example:

console.log("5" + 2);

Output:

52

Reason:

Number 2 is automatically converted to string.

Explicit Type Casting

Developer manually converts the type.

String to Number

Number("10")

Output:

10

Number to String

String(100)

Output:

"100"

Number to Boolean

Boolean(1)

Output:

true

Example

let age = "25";

console.log(Number(age));

Output:

25

Interview Tip

JavaScript is loosely typed, so automatic type conversion happens frequently.


7️⃣ Truthy & Falsy Values

When JavaScript evaluates a condition, values are treated as either Truthy or Falsy.

Falsy Values

There are only 8 Falsy values in JavaScript:

false
0
-0
0n
""
null
undefined
NaN

Truthy Values

Everything else is Truthy.

Examples:

"Hello"
1
[]
{}
true
"false"

Example

if ("Hello") {
console.log("Truthy");
}

Output:

Truthy

Important Interview Question

Is an empty array truthy or falsy?

Answer:

Truthy ✅

Example:

if ([]) {
console.log("Truthy");
}

Output:

Truthy


8️⃣ var vs let vs const

These are used to declare variables.

var

  • Function scoped
  • Can be redeclared
  • Can be reassigned
  • Hoisted

Example:

var a = 10;
var a = 20;

Allowed ✅

let

  • Block scoped
  • Cannot be redeclared
  • Can be reassigned

Example:

let age = 25;
age = 30;

Allowed ✅

const

  • Block scoped
  • Cannot be redeclared
  • Cannot be reassigned

Example:

const pi = 3.14;

pi = 3.141;

Error ❌

Quick Comparison

Feature var let const
Scope Function Block Block
Redeclare Yes No No
Reassign Yes Yes No
Hoisted Yes Yes Yes

Best Practice

Use:

  • const by default
  • let when value changes
  • Avoid var

9️⃣ What is JavaScript?

JavaScript is a high-level, interpreted, lightweight programming language used to create dynamic and interactive web pages.

Why JavaScript?

HTML → Structure

CSS → Styling

JavaScript → Functionality

Common Uses

  • Form Validation
  • Dark Mode
  • Image Sliders
  • API Calls
  • Dynamic Content Updates
  • Games
  • Real-Time Chat Applications

Example

Button click event:

document.getElementById("btn").addEventListener("click", () => {
alert("Button Clicked");
});

Where JavaScript Runs?

  • Browser
  • Server-side using Node.js

Interview Definition

JavaScript is a high-level, interpreted programming language used to make websites interactive and dynamic.


🔟 Mutable vs Immutable Data

Mutable

A mutable object can be modified after creation.

Examples:

  • Array
  • Object

Example:

let arr = [1, 2, 3];

arr.push(4);

console.log(arr);

Output:

[1, 2, 3, 4]

The original array was modified.

Immutable

An immutable value cannot be modified after creation.

Examples:

  • String
  • Number
  • Boolean

Example:

let str = "Hello";

str[0] = "Y";

console.log(str);

Output:

Hello

Reason:

Strings are immutable in JavaScript.

Another Example

let num = 10;

num = 20;

This creates a new value rather than modifying the original value.

Quick Summary

Mutable:

  • Array
  • Object

Immutable:

  • String
  • Number
  • Boolean
  • Null
  • Undefined
  • Symbol
  • BigInt

Interview Tip

Primitive data types are immutable, while Objects and Arrays are mutable because their contents can be changed after creation.

Top comments (0)