Skip to main content

JavaScript Objects

Objects the Basics: JavaScript Objects


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

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

Technical Response: There are seven primitive data types: text, integer, BigInt, Boolean, undefined, Symbol, and null. A primitive value gets directly represented at the language implementation's lowest level most of the time.


Are JavaScript primitive immutable or mutable?

View Answer:
Interview Response: All primitives are immutable and cannot be changed.

Technical Response: All primitives are unchanging and unchangeable. It is crucial to distinguish between a primitive and a variable with a primitive value. The variable can assign a new value, but it cannot get modified like objects, arrays, and functions can. A primitive can get swapped, but we cannot directly alter them.

Code Example:

// Using a string method does not mutate the string
var bar = 'baz';
console.log(bar); // baz
bar.toUpperCase();
console.log(bar); // baz

// Using an array method mutates the array
var foo = [];
console.log(foo); // []
foo.push('plugh');
console.log(foo); // ["plugh"]

// Assignment gives the primitive a new (not a mutated) value
bar = bar.toUpperCase(); // BAZ

What is the purpose of an object in JavaScript?

View Answer:
Interview Response: We use Objects to store keyed collections of various data and more complex entities.

Code Example:

let user = {
// an object
name: 'John', // by key "name" store value "John"
age: 30, // by key "age" store value 30
};
note

In JavaScript, objects penetrate almost every aspect of the language.


What are the two ways to implement an empty Object?

View Answer:
Interview Response: You can use an object constructor or an object literal.

Technical Response: We can create an empty object ("empty cabinet") using one of two syntaxes. You can use an object constructor or an object literal. The Object constructor gets called with the new keyword, and we should not confuse this with native Objects that should get called without the new keyword.

Code Example:

let user = new Object(); // "object constructor" syntax
let user = {}; // "object literal" syntax

What delimiter separates a key from the value in an Object?

View Answer:
Interview Response: You can separate Object key-value pairs with a colon.

Code Example:

let user = {
// an object
name: 'John', // key : value
age: 30, // by key "age" store value 30
};

Can a value be of any type in JavaScript?

View Answer:
Interview Response: Yes, since all values in JavaScript are dynamically typed and observed while the script executes.

What are the two ways to access a value in an Object?

View Answer:
Interview Response: In JavaScript, You can use dot (obj.property) and bracket (obj[property]) notation to access the object property values.

Technical Response: In JavaScript, the dot notation and bracket notation get used in accessing object attributes. The dot notation commonly gets used because it is easier to read and grasp and is less verbose. The primary distinction between dot notation and bracket notation is that bracket notation allows us to access object characteristics through variables.

Code Example:

let obj = {
cat: 'meow',
dog: 'woof',
};

// Dot Notation
let sound = obj.cat;
console.log(sound);
// meow

// Bracket Notation
let sound = obj['cat']; // Notice that cat is in ‘quotes’ (required)
console.log(sound);
// meow

Can you use multi-word property names in JavaScript?

View Answer:
Interview Response: Technically, JavaScript allows multi-word properties in objects, but this approach does not meet specifications because it can lead to errors. We should use camel-case as recommended by most style guides.

Technical Response: Technically, JavaScript enables multi-word properties in Objects, but it is not encouraged since it might create issues later in your code when you try to access it. The problem becomes evident when you try to access the property using dot notation. When naming functions, objects, attributes, and variables in JavaScript, you should always use the camelCase naming style.

Code Example:

let user = {
name: "John",
age: 30,
"likes birds": true, // multi-word property name must be quoted
};

console.log(user['likes birds']); // return true
console.log(user.likes birds); // returns a SyntaxError

Is JavaScript able to implement computed properties using object-literal notation?

View Answer:
Interview Response: Yes, you may assign the expression as a property to an object without first creating it with object-literal notation.

Code Example:

// Complex Expressions inside of square brackets
function objectify(key, value) {
return {
[key]: value,
};
}

objectify('name', 'Tyler'); // { name: 'Tyler' }

//////////////////////////

let fruit = 'apple';
let bag = {
[fruit + 'Computers']: 5, // bag.appleComputers = 5
};

console.log(bag.appleComputers); // logs 5

Are there any restrictions on Object property names?

View Answer:
Interview Response: No, there are no known restrictions on Object property names, but we should avoid reserved words even though we can technically use them.

Technical Response: There are no known constraints on naming Object properties. However, we should not use reserved keywords in most JavaScript components. It is possible to utilize reserved keywords as property names without making a mistake, although it is not encouraged. They can be strings or symbols (a specific form of identifier).

Code Example:

// these properties are all right
let obj = {
for: 1,
let: 2,
return: 3,
};

alert(obj.for + obj.let + obj.return); // 6

What is the minor snag with the special property name __proto__?

View Answer:
Interview Response: You should not set the special property name `__proto__` to a non-object value such as a primitive value. It can have unexpected results.

Code Example:

let obj = {};
obj.__proto__ = 5; // assign a number
alert(obj.__proto__); // [object Object] - the value is an object, didn't work as intended

Why does the "in" operator exist? Isn't it enough to compare against undefined?

View Answer:
Interview Response: The “in” operator is used to check if a property exists or loop over object properties. Using it to compare against undefined can have less than truthy results.

Note: We should not use the "in" operator to loop over arrays; not a recommended approach.

Technical Answer:The comparison with undefined, on the other hand, works most of the time. However, there is one exception where it fails yet "in" works ideally. We may use the "in" operator to check if a property exists.

Code Example:

let obj = {
test: undefined,
};

console.log(obj.test); // returns undefined, so - no such property?

console.log(obj.test === undefined); // true

console.log('test' in obj); // true, the property does exist!

Are object properties in order? In other words, do we get all attributes in the same order they get introduced if we loop over an object? Can we put our faith in this?

View Answer:
Interview Response: The short answer is that they get ordered in a particular way, integer properties get sorted, and others appear in creation order. This behavior is not a reliable approach, and we are seeking a specific numbered order.

Code Example:

// Integers are in sorted order
let codes = {
49: 'Germany',
41: 'Switzerland',
44: 'Great Britain',
// ..,
1: 'USA',
};

for (let code in codes) {
alert(code); // 1, 41, 44, 49
}

let user = {
name: 'John',
surname: 'Smith',
};
user.age = 25; // add one more

// non-integer properties are listed in the creation order
for (let prop in user) {
alert(prop); // name, surname, age
}

Explain what an integer property is in JavaScript?

View Answer:
Interview Response: The "integer property" term here means a string that converts to and from an integer without a change.

Code Example:

// Math.trunc is a built-in function that removes the decimal part
alert(String(Math.trunc(Number('49')))); // "49", same, integer property
alert(String(Math.trunc(Number('+49')))); // "49", not same "+49" ⇒ not integer property
alert(String(Math.trunc(Number('1.2')))); // "1", not same "1.2" ⇒ not integer property