Methods of Primitives
Data Types: Methods of Primitives
What is a primitive in JavaScript?
View Answer:
Interview Response: A primitive is a basic data type that represents a single, immutable value, such as a string, number, boolean, null, undefined, symbol, or bigint.
Code Example:
Here is an example that shows how primitive types work in JavaScript:
let stringPrimitive = "Hello, World!"; // String primitive
let numberPrimitive = 123; // Number primitive
let booleanPrimitive = true; // Boolean primitive
let nullPrimitive = null; // Null, which is a primitive with one null value
let undefinedPrimitive = undefined; // Undefined, a primitive that represents no value or no object
console.log(typeof stringPrimitive); // "string"
console.log(typeof numberPrimitive); // "number"
console.log(typeof booleanPrimitive); // "boolean"
console.log(typeof nullPrimitive); // "object" (this is a known JavaScript oddity, typeof null returns "object")
console.log(typeof undefinedPrimitive); // "undefined"
// Bonus: Symbol type, introduced in ES6
let symbolPrimitive = Symbol("symbol");
console.log(typeof symbolPrimitive); // "symbol"
// Bonus: BigInt type, introduced in ES10
let bigIntPrimitive = 10n;
console.log(typeof bigIntPrimitive); // "bigint"
Each console.log
statement logs the type of each primitive to the console. Note the typeof
operator in JavaScript is used to find out the type of a JavaScript variable.
What is autoboxing in relation to primitive values in JavaScript?
View Answer:
Interview Response: Autoboxing in JavaScript is the automatic conversion of primitive data types (string, number, boolean, etc.) into their corresponding object wrappers (String, Number, Boolean) when a method is invoked on them.
Technical Details: In JavaScript, autoboxing is the process by which primitive types are automatically converted to their associated object types (wrapper objects) when a method is called on them. This is possible because JavaScript has built-in constructor functions, String, Number, Boolean, and Symbol, which correspond to the string, number, boolean, and symbol primitive types. These constructor functions can create objects that behave similarly to their respective primitives.
For instance, when you call a method on a string primitive, JavaScript automatically "boxes" the string primitive in a String object so that the method can be executed. Once the operation is completed, it is converted back ("unboxed") to a primitive. This process is abstracted away from the developer, hence the term "autoboxing".
For instance, when you call a method on a string primitive, JavaScript automatically "boxes" the string primitive in a String object so that the method can be executed. Once the operation is completed, it is converted back ("unboxed") to a primitive. This process is abstracted away from the developer, hence the term "autoboxing".
What are the methods of primitives in JavaScript?
View Answer:
Interview Response: Each primitive type has a set of methods associated with it that allow you to perform common operations on values of that type. For example, the toUpperCase() method can be used on strings to convert them to uppercase.
Technical Details: In JavaScript, primitives are not objects and therefore cannot have methods. However, JavaScript has a feature known as "autoboxing", where it allows you to access string, number, boolean and symbol primitives as if they were objects and provides several methods you can use on these primitive values. This is achieved by automatically converting the primitive to an object wrapper when a method is called, then discarding the object wrapper when the operation is complete.
Code Example:
Here are examples for each primitive type:
String methods:
let text = 'Hello, World!';
console.log(text.toUpperCase()); // "HELLO, WORLD!"
console.log(text.includes('World')); // true
console.log(text.charAt(0)); // "H"
console.log(text.split(',')); // ["Hello", " World!"]
Number methods:
let num = 12345.6789;
console.log(num.toFixed(2)); // "12345.68" - format number with 2 decimal places
console.log(num.toExponential(3)); // "1.235e+4" - returns a string, with the number written into exponential notation, a rounded and followed by "e", followed by the number of zeros.
console.log(num.toString()); // "12345.6789" - convert number to string
Boolean methods:
let bool = true;
console.log(bool.toString()); // "true"
note
Note that null and undefined primitive types do not have any associated methods. Also remember, these methods do not change the original value, but rather return a new value.
Can you modify a primitive value using its methods in JavaScript?
View Answer:
Interview Response: No, because primitives are immutable, you cannot modify them using methods. Instead, methods return new values.
What are the seven (7) primitive JavaScript data types?
View Answer:
Interview Response: The seven JavaScript primitives include String, Symbol, Boolean, BigInt, number, null, and undefined data types. These represent basic values that can be manipulated in JavaScript code.
What are JavaScript's five (5) primitive wrapper objects?
View Answer:
Interview Response: In JavaScript, there are five primitive wrapper objects, which are used to wrap the corresponding primitive data types. These include String, Symbol, BigInt, Boolean, and Number, allowing access to additional methods and properties.
Code Example:
let language = 'JavaScript';
let s = language.substring(4);
console.log(s); // logs Script
// WHAT'S ACTUALLY HAPPENING BEHIND THE SCENES!
let language = 'JavaScript';
// behind the scenes of the language.substring(4);
let tmp = new String(language);
str = temp.substring(4);
temp = null;
What is the reason for an Object not being considered a primitive data type in JavaScript?
View Answer:
Interview Response: Primitive data types have a single value, while objects can hold multiple values in collections and more intricate 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, an object can store a function as a property. In JavaScript, functions are first-class objects, enabling them to be assigned as object properties.
Code Example:
let john = {
name: 'John',
sayHi: function () {
console.log('Hi buddy!');
},
};
john.sayHi(); // Hi buddy!
Does a primitive remain primitive when used in conjunction with one of its methods?
View Answer:
Interview Response: When a primitive is used in conjunction with one of its methods, it is temporarily converted into an object. After execution, it reverts back to a primitive.
Code Example:
let str = 'Hello';
console.log(str.toUpperCase()); // returns string "HELLO"
Some languages like Java allow us to explicitly create “wrapper objects” for primitives. Is this a best practice in JavaScript?
View Answer:
Interview Response: No, creating wrapper objects for primitives in JavaScript is not considered a best practice, as it can result in unnecessary memory usage and performance overhead.
Technical Response: Technically, this can be done in JavaScript, but it is not recommended based on the specifications. There are several unintended consequences of using this practice, which should be avoided. A best practice is to use Number and Boolean object wrappers without the new operator.
Code Example:
console.log(typeof 0); // "number"
console.log(typeof new Number(0)); // "object"! – not a number Huh!
// Objects are always truthy in if, so here the console.log will show up:
let zero = new Number(0);
if (zero) {
// zero is true, because it's an object
console.log('zero is truthy!?!');
}
// this is entirely valid:
let num = Number('123'); // convert a string to number
What is the difference between a primitive value and a wrapper object in JavaScript?
View Answer:
Interview Response: A primitive value is an immutable value representing a single, simple data entity, while a wrapper object is a mutable object used to represent and manipulate the corresponding primitive value.
Can the null and undefined primitive data types to have methods in JavaScript?
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:
console.log('Hello'.test); // return undefined, but shows no error
console.log(null.test); // returns type error
console.log(undefined.test); // returns type error
Can you create your own methods for primitive values in JavaScript?
View Answer:
Interview Response: Yes, you can create your own methods for primitive values in JavaScript by adding properties and methods to the prototype of the corresponding wrapper object (e.g. Number.prototype, String.prototype).
Code Example:
String.prototype.customMethod = function() {
return "Custom method called on: " + this;
};
var message = "Hello, JavaScript!"; // primitive string
console.log(message.customMethod()); // Output: "Custom method called on: Hello, JavaScript!"
What are some common string methods in JavaScript?
View Answer:
Interview Response: Some common string methods in JavaScript include toUpperCase(), toLowerCase(), slice(), indexOf(), replace(), concat(), and trim().
What are some common number methods in JavaScript?
View Answer:
Interview Response: Some common number methods in JavaScript include toFixed(), toPrecision(), toString(), parseInt(), and parseFloat().