Questions
10 of 32
1What is the constructor function?
2What are Objects?
3What are the properties of an object?
4How does javascript implements object oriented programming?
5What are the key differences between javascript’s Object-based Inheritance and Conventional Class-based Inheritance?
6What is a prototype in JavaScript?
7How we can implement prototype-based inheritance?
8Explain the prototype chain and its role in property lookup.
9What is the difference between an object's `__proto__` and a constructor function's `prototype` property and [[Prototype]]?
10What are object wrappers for primitive type?
11Can you explain the use of object wrappers with examples?
12Explain how the `instanceof` operator works.
13What are different methods to create an object?
14What is an object initializer?
15What does the `Object.create()` method do?
16How are constructor functions used to create objects with shared properties and methods?
17Show an example of ES6 classes to create objects and inherit properties?
18What are factory functions in JavaScript ?
19What are Object constructors in JavaScript ?
20How can you add new properties and methods to the prototype of an existing constructor function?
21What is prototype pollution, and how can it be a security risk in JavaScript?
22Is modifying or extending built-in object prototypes recommended (e.g., adding a method to `Array.prototype`)? Why or why not?
23What happens when you try to access a property that doesn't exist on an object?
24How can you prevent or mitigate prototype pollution vulnerabilities in your code?
25How does the `Object.prototype.hasOwnProperty()` method work?
26How can you optimize prototype chain lookup for better performance?
27Can you explain how `Object.prototype.constructor` property works?
28What is the difference between modifying the prototype of an object and adding a property directly to the object?
29How does JavaScript handle circular references in object prototypes?
30What is static dispatching in object-oriented programming?
31What is the purpose of the `Object.keys()` method?
32Write a function flattenObject(obj) that flattens a deeply nested object into a single-level object, using dot notation for nested keys.
10 / 32

What are object wrappers for primitive type?

Difficulty: 5/10
primitive wrappers, type coercion, performance

object wrappers are objects that encapsulate primitive data types (such as numbers, strings, and booleans) and provide them with additional methods and properties. These wrappers allow you to treat primitive values as objects, enabling you to access and manipulate their values more conveniently. All primitive types, except null and undefined, have their corresponding object wrapper types,

The 'Autoboxing' Process
  1. 1

    When you try to access a property or method on a primitive, JavaScript temporarily wraps that primitive in a special object so you can use its powers.

  2. 2

    The Trigger: You call .toUpperCase() on a string.

  3. 3

    The Wrap: JavaScript creates a temporary String object: new String('gemini').

  4. 4

    The Execution: It runs the method on that object and returns the result.

  5. 5

    The Cleanup: JavaScript immediately throws the object away (garbage collection) to save memory.

Why not just use Objects for everything?
  1. 1

    Performance: Objects are heavy. Primitives are 'passed by value' and stored directly on the Stack. Primitives are lightning-fast and memory efficient.

  2. 2

    Objects are stored on the Heap, require memory addresses (pointers), and consume more resources.

  3. 3

    By using wrappers, JavaScript gives you the speed of primitives with the functionality of objects only when you actually need it.

A common mistake is trying to assign a property to a primitive. It won't throw an error (in non-strict mode), but it won't work because of the 'temporary' nature of the wrapper.

Scenario Questions

0-2 years experience

  1. 1If you receive a JSON payload where a numeric field is sometimes a string, how would you use Number or the Number wrapper to ensure you have a proper number before calculations?
  2. 2What will console.log(typeof new Boolean(false)) output and why? How does it differ from typeof false?
  3. 3When you call 'hello'.length, JavaScript temporarily creates a wrapper object. Explain what would happen if you tried to add a new property to that string, e.g., 'hello'.custom = 5; and then read it back.

2-5 years experience

  1. 1Your team noticed that a function sometimes throws 'TypeError: Cannot read property "toFixed" of undefined' when processing user input. The input may be a string or a number. Walk through how primitive wrappers could be causing this and how you'd fix it.
  2. 2In a codebase you see code like `if (new Number(value) > 10)`. Discuss the pros and cons of using the Number wrapper here versus a plain numeric conversion, and what subtle bugs might arise.
  3. 3During a performance profiling session you see many allocations of Number objects. Explain why that might be happening and how you would refactor to reduce GC pressure.

5-8 years experience

  1. 1You are designing a logging library that must accept any primitive value and preserve its type when serializing. How would you leverage object wrappers to detect the original primitive type without losing information?
  2. 2A large microservice processes billions of numeric calculations per day. The current implementation uses `new Number()` in several hot loops. Evaluate the impact on latency and memory, and propose a migration plan.
  3. 3When integrating with a third‑party API that expects objects with methods like `toString` and `valueOf`, you need to wrap primitive values. Describe how you'd design a utility that safely creates wrapper objects only when needed, avoiding accidental prototype pollution.

8+ years experience

  1. 1Your organization is moving from a legacy codebase that heavily relies on primitive wrapper objects (e.g., `new Boolean`, `new String`) to a modern TypeScript codebase with strict typing. Outline an architectural migration strategy, including tooling, lint rules, and runtime compatibility concerns.
  2. 2Several teams share a common utility library that provides functions like `isNumber(value)` which internally uses `Object.prototype.toString.call(value)`. Discuss the trade‑offs of using wrapper detection versus `typeof` checks in a polyglot environment where some services are written in Node.js and others in Deno.
  3. 3Imagine you need to design a serialization format that can round‑trip both primitive values and their wrapper objects, preserving the distinction (e.g., differentiate between `5` and `new Number(5)`). How would you approach this at the system level, considering backward compatibility and performance?

Follow-up Questions

  • Can you give an example where using a wrapper caused a bug in equality checks?
  • How would you detect at runtime whether a value is a primitive or a wrapper without using `instanceof`?
  • What impact do wrapper objects have on garbage collection in a tight loop?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.