JavaScript Objects and Arrays Quiz

Find out how much you know about creating and manipulating objects, accessing and modifying array elements, and working with array methods in JavaScript.

Question 1/15

How do you check if a JavaScript object has a specific value?

Question 2/15

What does the Object.entries() method return?

Question 3/15

How do you concatenate two or more JavaScript arrays?

Question 4/15

The Array.isArray() method can be used to determine if an object is an instance of an array-like object

Question 5/15

What is the default separator used by the join() method when no separator is specified as an argument?

Question 6/15

How do you remove duplicates from a JavaScript array?

Question 7/15

How do you check if a JavaScript object is empty?

Question 8/15

How do you remove the last element from a JavaScript array?

Question 9/15

How do you check if a JavaScript object has a specific property?

Question 10/15

How do you sort a JavaScript array in descending order?

Question 11/15

How do you get the length of a JavaScript array?

Question 12/15

How do you get the length of a JavaScript object?

Question 13/15

What does the Object.isFrozen() method return when called on a non-object value?

Question 14/15

What does the array.some() method return?

Question 15/15

How do you remove a property from a JavaScript object?
You did not answer any questions correctly.

Your Answers

Question 1/15

How do you check if a JavaScript object has a specific value?

To check if a JavaScript object has a specific value, we can use the Object.values() method to get an array of the object's values, and then use the includes() method to check if the value is in the array.

Example:

var myObj = {
  name: "John",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Anytown",
    state: "CA"
  }
};
console.log(Object.values(myObj).includes("John")); // Output: true
console.log(Object.values(myObj).includes("Jane")); // Output: false 

In this example, we use the Object.values() method to get an array of the values in the myObj object, and then use the includes() method to check if the value "John" is in the array. The method returns true, because "John" is one of the values in the array.

Question 2/15

What does the Object.entries() method return?

The Object.entries() method is used to return an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop. Each key-value pair is represented as an array containing two elements: the key as the first element, and the corresponding value as the second element.

For example, if we have an object myObj that looks like this:

var myObj = {
  name: "John",
  age: 30,
  city: "New York"
};

We can use the Object.entries() method to convert this object to an array of key-value pairs, like this:

var entriesArray = Object.entries(myObj);
console.log(entriesArray);

The output in the console will be an array of key-value pairs:

[  ["name", "John"],
  ["age", 30],
  ["city", "New York"]
]

Each key-value pair is represented as an array containing two elements: the key as the first element, and the corresponding value as the second element.

Question 3/15

How do you concatenate two or more JavaScript arrays?

To concatenate two or more JavaScript arrays, we can use the concat() method as follows: array1.concat(array2, array3, ...). This method creates a new array that contains all the elements from the original arrays in the order they are provided.

Example:

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var array3 = [7, 8, 9];
var newArray = array1.concat(array2, array3);
console.log(newArray); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In this example, we create three arrays (array1, array2, and array3) and then concatenate them using the concat() method. We store the result in a new array called newArray and log it to the console using console.log().

Question 4/15

The Array.isArray() method can be used to determine if an object is an instance of an array-like object

The Array.isArray() method is specifically designed to check whether a given value is an array. It will return true if the argument passed to it is an instance of an array, and false otherwise.

However, there are some objects that have array-like properties, such as a length property and numeric indices, but are not actually instances of an array. These objects are sometimes called "array-like" objects, but they are not arrays and should not be treated as such.

Question 5/15

What is the default separator used by the join() method when no separator is specified as an argument?

If no separator is specified as an argument when using the join() method in JavaScript, the default separator used is a comma (",").

Question 6/15

How do you remove duplicates from a JavaScript array?

To remove duplicates from a JavaScript array, we can use the filter() method with a custom filtering function that keeps only the first occurrence of each unique element in the array.

Example:

var myArray = [1, 2, 2, 3, 4, 4, 5];
var uniqueArray = myArray.filter((value, index, self) => self.indexOf(value) === index);
console.log(uniqueArray); // Output: [1, 2, 3, 4, 5]

In this example, we use the filter() method with a custom filtering function that keeps only the first occurrence of each unique element in the array. We then store the resulting array in a new array called uniqueArray and log it to the console using console.log().

Question 7/15

How do you check if a JavaScript object is empty?

To check if a JavaScript object is empty, we can use the Object.keys() method to get an array of the object's keys, and then check if the length of that array is equal to 0.

Example:

var myObj = {};
console.log(Object.keys(myObj).length === 0); // Output: true

In this example, we use the Object.keys() method to get an array of the keys in the myObj object, and then check if the length of that array is equal to 0. Since myObj is an empty object, the expression returns true.

Question 8/15

How do you remove the last element from a JavaScript array?

The pop() method in JavaScript is used to remove the last element from an array and return the removed element. The original array is modified by removing the last element from the end of the array.

Here's an example of how to use the pop() method:

var myArray = [1, 2, 3, 4, 5];
var lastElement = myArray.pop(); // remove the last element from the array
console.log(myArray); // Output: [1, 2, 3, 4]
console.log(lastElement); // Output: 5

In this example, we first create an array called myArray that contains five elements. We then call the pop() method on myArray, which removes the last element (5) from the end of the array and returns it. We store the removed element in a variable called lastElement. Finally, we log both the modified myArray (which now has only four elements) and the removed last element (5) to the console using console.log().

Question 9/15

How do you check if a JavaScript object has a specific property?

To check if a JavaScript object has a specific property, we can use the hasOwnProperty() method as follows: object.hasOwnProperty(property). This returns true if the object has the specified property, and false otherwise.

Example:

var myObj = {
  name: "John",
  age: 30
};
console.log(myObj.hasOwnProperty("name")); // Output: true

Question 10/15

How do you sort a JavaScript array in descending order?

To sort a JavaScript array in descending order, we can use the sort() method with a custom sorting function that sorts the elements in reverse order.

Example:

var myArray = [3, 1, 4, 2];
myArray.sort((a, b) => b - a);
console.log(myArray); // Output: [4, 3, 2, 1] 

In this example, we use the sort() method with a custom sorting function that sorts the elements in reverse order (b - a). This sorts the myArray array in descending order.

Question 11/15

How do you get the length of a JavaScript array?

To get the length of a JavaScript array, we can use the length property as follows: array.length. This returns the number of elements in the array.

Example:

var myArray = [1, 2, 3];
console.log(myArray.length); // Output: 3

Question 12/15

How do you get the length of a JavaScript object?

Unlike arrays, JavaScript objects do not have a length property. However, we can use the Object.keys() method to get an array of the object's keys, and then use the length property of that array to get the number of keys in the object.

Example:

var myObj = {
  name: "John",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Anytown",
    state: "CA"
  }
};
console.log(Object.keys(myObj).length); // Output: 3

Question 13/15

What does the Object.isFrozen() method return when called on a non-object value?

The Object.isFrozen() method is used to determine whether an object is frozen or not. If the method is called on a non-object value, such as a primitive value like a number or a string, a TypeError will be thrown. This is because the isFrozen() method can only be used with objects, and not with other types of values.

Question 14/15

What does the array.some() method return?

array.some() method tests whether at least one element in the array passes the test implemented by the provided function and returns a boolean value. The provided function is executed on each element of the array until it returns true for at least one element, or until all elements have been tested. If the function returns true for at least one element, array.some() returns true; otherwise, it returns false. This behavior is useful for checking whether any element in an array meets a certain condition.

Question 15/15

How do you remove a property from a JavaScript object?

To remove a property from a JavaScript object, we can use the delete operator as follows: delete object.property. This removes the specified property from the object.

Take Another Quiz

Advanced CSS Quiz

Advanced CSS Quiz designed to test your knowledge of advanced CSS concepts, including layout, positioning, blending, and responsive design. Start quiz

Agile Methodologies Quiz

Test your understanding of Agile methodologies, including Scrum, Kanban, and Lean. Understand how to use Agile principles to enhance the efficiency and productivity of your software development projects. Start quiz

Basic HTML & CSS Quiz

From basic tags and selectors to layout and positioning, this quiz covers essential topics that every beginner frontend web developer should know. Start quiz

Basic JavaScript Quiz

Test your understanding of the fundamental concepts and syntax of JavaScript, including variables, data types, conditionals, loops, and functions. Start quiz

CSS Box Model Quiz

Identify the correct box model properties for various layout scenarios and determine how changes to these properties affect the appearance of an element. Start quiz

CSS Flexbox Quiz

Test your knowledge of the CSS Flexbox layout module with multiple choice questions and their explanations. Start quiz