TGViewer
Web Development - HTML, CSS & JavaScript Web Development - HTML, CSS & JavaScript @javascript_courses ยท 55.7K subscribers
Post #3609 1.96K
๐Ÿš€ JavaScript Interview Questions with Answers โ€” Part 4

31. How do you create objects in JavaScript?

There are several ways to create objects in JavaScript.

1. Object Literal

const user = {
name: "John",
age: 25
};


2. new Object()

const user = new Object();
user.name = "Frey";
user.age = 35;


3. Constructor Function

function User(name, age) {
this.name = name;
this.age = age;
}
const user = new User("John", 25);


4. Class

class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const user = new User("John", 25);


Interview Tip:

Object literals are usually preferred for simple objects, while classes or constructor functions are useful when creating many similar objects.

32. What is object destructuring?

Object destructuring allows you to extract properties from an object and store them in variables.

Example:

const user = {
name: "John",
age: 25,
city: "New York"
};
const { name, age } = user;
console.log(name);
console.log(age);


Output:

John

25

Rename Variables:

const { name: userName } = user;
console.log(userName);


Default Value:

const { country = "India" } = user;
console.log(country);


33. What is the spread operator (...)?

The spread operator expands the elements of an iterable or properties of an object.

Array Example:

const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
console.log(newNumbers);


Output:

[1, 2, 3, 4, 5]

Object Example:

const user = {
name: "John",
age: 25
};

const updatedUser = {
...user,
city: "New York"
};


Common Uses:

โœ… Copy arrays

โœ… Merge arrays

โœ… Copy objects

โœ… Merge objects

โœ… Pass values to functions

34. What is the rest operator?

The rest operator (...) collects multiple values into a single array or object.

Example:

function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(10, 20, 30));


Output: 60

Here, ...numbers collects all arguments into an array.

Important Interview Point:

The same ... syntax has different purposes:

Spread โ†’ expands values: const arr2 = [...arr1];

Rest โ†’ collects values: function test(...args) {}

35. What are default parameters?

Default parameters allow you to provide a default value when an argument is not passed or is undefined.

Example:

function greet(name = "Guest") {
console.log(`Hello ${name}`);
}
greet();


Output: Hello Guest

If a value is provided: greet("Deepika"); โ†’ Hello Deepika

Multiple Defaults:

function createUser(name = "Guest", age = 18) {
console.log(name, age);
}


36. What is optional chaining (?. )

Optional chaining allows you to safely access nested properties without throwing an error when an intermediate value is null or undefined.

Without Optional Chaining:

const user = {};
console.log(user.address.city); // Error


With Optional Chaining:

console.log(user.address?.city); // undefined
  • โค 1
More from @javascript_courses
  1. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  2. Oct 4, 2026DSA Handwritten Notes
  3. Sep 28, 2026To learn web design and development from basic to advanced levels, you can follow these stโ€ฆ
  4. Sep 27, 202610 Websites Every Developer & AI Enthusiast Should Bookmark โœ… roadmap.sh โ€“ Step-by-step leโ€ฆ
  5. Sep 26, 2026๐Ÿ”น Escape Characters 2๏ธโƒฃ5๏ธโƒฃ New Line You can use \n and \t: const message1 = "Hello\nWorldโ€ฆ
  6. Sep 26, 2026const fileName = "report.pdf"; console.log(fileName.endsWith(".pdf")); // true ๐Ÿ”น Findingโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’