Question 1/15
Question 2/15
Question 3/15
Question 4/15
Question 5/15
Question 6/15
Question 7/15
Question 8/15
Question 9/15
Question 10/15
Question 11/15
Question 12/15
Question 13/15
Question 14/15
Question 15/15
Question 1/15
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 2/15
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 3/15
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 4/15
If no separator is specified as an argument when using the join() method in JavaScript, the default separator used is a comma (",").
Question 5/15
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 6/15
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 7/15
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 8/15
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.
Question 9/15
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 10/15
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 11/15
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 12/15
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 13/15
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 14/15
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
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.