Skip to main content

Methods of Primitives

Data Types: Methods of Primitives

What are the seven (7) primitive JavaScript data types?

View Answer:
Interview Response: String, Symbol, Boolean, BigInt, number, null, and undefined.

Technical Response: The seven primitive data types include the string, number, BigInt, Boolean, undefined, symbol, and null data types. Most of the time, a primitive value is represented directly at the lowest level of the language implementation.

What are JavaScript's five (5) primitive wrapper objects?

View Answer:
Interview Response: String, Symbol, BigInt, Boolean, and Number.

Technical Response: The five primitive wrapper objects include the String, Number, BigInt, Boolean, and Symbol.

Code Example:

let language = 'JavaScript';
let s = language.substring(4);
console.log(s); // logs Script


let language = 'JavaScript';

// behind the scenes of the language.substring(4);
let tmp = new String(language);
str = temp.substring(4);
temp = null;

Why isn’t an Object a primitive data type?

View Answer:
Interview Response: A primitive data type has only a single value. On the other hand, objects can contain more than one value stored in collections and more complex structures.

Code Example:

// String primitive
let str = 'hello'; // holds one value

// Object is Special
let user = {
name: 'Jane', // holds multiple key/value pairs.
age: 30,

Can an object store a function as a property?

View Answer:
Interview Response: Yes, a function gets used as a property of an object, and it is more commonly called an object method.

Code Example:

let john = {
name: 'John',
sayHi: function () {
alert('Hi buddy!');

john.sayHi(); // Hi buddy!

Does a primitive remain a primitive or return a primitive value when you use it in conjunction with one of its methods?

View Answer:
Interview Response: Yes, a primitive remains a primitive regardless of what method gets called on it.

Technical Response: Yes, a primitive remains a primitive. For instance, a string method str.toUpperCase() exists that returns a capitalized string. The type of the returned value remains a string.

Code Example:

let str = 'Hello';

alert(str.toUpperCase()); // HELLO

Some languages like Java allow us to explicitly create “wrapper objects” for primitives using a syntax like new Number(1) or new Boolean(false). Is this a best practice in JavaScript?

View Answer:
Interview Response: Technically, using the “new” syntax can be done but is not recommended because it returns an object and not the primitive value as intended.

Technical Response: Technically, this gets done in JavaScript, but it is not recommended based on the specifications. There are several unintended consequences of using this practice, which should get avoided. A best practice is to use Number and Boolean object wrappers without the new operator.

Code Example:

alert(typeof 0); // "number"

alert(typeof new Number(0)); // "object"! – not a number Huh!

// Objects are always truthy in if, so here the alert will show up:

let zero = new Number(0);

if (zero) {
// zero is true, because it's an object
alert('zero is truthy!?!');

// this is entirely valid:

let num = Number('123'); // convert a string to number

Do the null and undefined primitive data types have methods?

View Answer:
Interview Response: No, they are considered the most primitive data types. They both have no wrapper objects and do not provide any methods.

Code Example:

alert('Hello'.test); // return undefined, but shows no error

alert(null.test); // returns type error

alert(undefined.test); // returns type error