Answer: A) 1
Explanation: The nodeType property returns 1 for element nodes, 3 for text nodes, and 8 for comment nodes.
Answer: B) Text node
Explanation: A nodeType of 3 corresponds to text nodes, which often include whitespace or actual text.
Answer: D) They can affect DOM navigation
Explanation: Whitespace and text nodes can interfere with child node indexing and traversal.
Answer: B) 8
Explanation: Comment nodes in the DOM have a nodeType of 8.
if (node.nodeType === 3)
if (node.nodeType === 1)
if (node.nodeType == "element")
if (node.type === 1)
Answer: B) if (node.nodeType === 1)
Explanation: The nodeType for element nodes is 1, and it should be compared using strict equality.
someElement.firstChild.nodeType
if the element starts with a text node?
Answer: B) 3
Explanation: If the first child of an element is a text node (including whitespace), firstChild.nodeType
will return 3.
Answer: C) Text
Explanation: Text nodes, especially whitespace, can be unexpected and cause issues with childNode indexing.
Answer: C) To ensure correct operations on intended node types
Explanation: By checking nodeType, developers avoid errors like trying to manipulate text nodes as if they were element nodes.
node.nodeType === 8
evaluate?
Answer: B) Whether node is a comment
Explanation: nodeType 8 refers to comment nodes in the DOM structure.
getElementById()
getElementsByTagName()
querySelectorAll()
Answer: D) All of the above
Explanation: These methods return only element nodes, automatically ignoring text and comment nodes.
document.getElementsByClassName("myClass")
return?
Answer: C) An HTMLCollection
Explanation: The method returns a live HTMLCollection of all elements with the specified class name.
querySelector()
querySelectorAll()
getElementsByTagName()
getElementsByClassName()
Answer: A) querySelector()
Explanation: querySelector()
returns the first element that matches a specified CSS selector.
getElementsByClassName()
getElementsByTagName()
querySelectorAll()
getElementById()
Answer: C) querySelectorAll()
Explanation: querySelectorAll()
returns a static NodeList, unaffected by later DOM changes.
document.querySelectorAll("p")
document.getElementsByTagName("p")
Answer: C) Both A and B
Explanation: Both methods retrieve all paragraph elements, though one returns a static list, the other live.
querySelector()
?
Answer: D) All of the above
Explanation: querySelector()
accepts any valid CSS selector, including ID, class, tag, and complex selectors.
document.querySelectorAll(".item")
return?
Answer: B) NodeList
Explanation: This method returns a static NodeList of all elements with the class .item
.
querySelector("header")
querySelector("#header")
querySelector(".header")
querySelector("@header")
Answer: B) querySelector("#header")
Explanation: The hash symbol (#) is used to select elements by ID in CSS selectors.
getElementsByClassName
and querySelectorAll
?
Answer: B) One is live, the other static
Explanation: getElementsByClassName()
returns a live collection, while querySelectorAll()
returns a static NodeList.
Array.from()
toArray()
slice()
NodeList.array()
Answer: A) Array.from()
Explanation: Array.from()
is the recommended way to convert NodeLists into real arrays.
querySelectorAll("ul li")
?
Answer: C) It selects all <li> inside any <ul>
Explanation: The selector matches all <li>
elements that are descendants of any <ul>
.
nodeName
property return?
Answer: C) The name of the node in uppercase
Explanation: nodeName
returns the name of the node in uppercase for element nodes (e.g., "DIV", "P").
element.nodeName
return for a <p> tag?
Answer: C) "P"
Explanation: The nodeName
of a <p> element is "P", as it's always returned in uppercase.
nodeName
of a text node?
Answer: A) #TEXT
Explanation: Text nodes return #TEXT
as their nodeName
.
nodeName
in DOM manipulation?
Answer: C) To identify the kind of node being dealt with
Explanation: nodeName
helps identify what kind of node you're working with (e.g., "DIV", "#TEXT").
nodeName
?
Answer: C) It's always uppercase for element nodes
Explanation: The nodeName
of element nodes is always returned in uppercase letters.
nodeName
?getNodeType()
Answer: B) Compare it to "#TEXT"
Explanation: To confirm a text node, compare its nodeName
to "#TEXT".
document.body.nodeName
return?
Answer: B) "BODY"
Explanation: document.body
targets the body element, and nodeName
returns "BODY".
element.nodeName === "#TEXT"
, what should you avoid?
Answer: D) All of the above
Explanation: Text nodes cannot have attributes, children, or innerHTML.
nodeName
?
Answer: A) Checking tag type before applying changes
Explanation: nodeName
helps determine an element's type before manipulation.
Answer: B) "#COMMENT"
Explanation: HTML comment nodes have the nodeName
value of "#COMMENT".
document.querySelector()
document.getElementById()
document.getElementsByTagName().length
document.countElements()
Answer: C) document.getElementsByTagName().length
Explanation: getElementsByTagName()
returns a live HTMLCollection, and you can count elements using its length
.
document.getElementsByClassName("p").length
document.querySelectorAll("p")
document.getElementsByTagName("p").length
document.p.length
Answer: C) document.getElementsByTagName("p").length
Explanation: To count <p>
tags, use getElementsByTagName("p").length
.
document.getElementsByTagName("li").length
return?
Answer: B) Number of list items
Explanation: getElementsByTagName("li")
targets all <li>
elements.
querySelectorAll()
getElementsByTagName()
Array.from()
getElementById()
Answer: B) getElementsByTagName()
Explanation: The result of getElementsByTagName()
updates automatically with DOM changes.
<div>
elements into a variable?let divs = document.getElementByTagName("div")
let divs = document.getElementsByClass("div")
let divs = document.getElementsByTagName("div")
let divs = document.divs
Answer: C) let divs = document.getElementsByTagName("div")
Explanation: This correctly stores all <div>
elements into a variable.
document.getElementsByTagName("*").length
return?
Answer: B) Number of all elements in the document
Explanation: The asterisk *
is a wildcard that selects all tags.
getElementsByName()
getElementByClass()
getElementsByClassName().length
countByClass()
Answer: C) getElementsByClassName().length
Explanation: getElementsByClassName()
targets elements with the given class.
getElementsByTagName()
getElementsByClassName()
querySelectorAll()
getElementById()
Answer: C) querySelectorAll()
Explanation: querySelectorAll()
returns a static list unaffected by DOM changes.
getElementsByTagName
return?
Answer: B) HTMLCollection
Explanation: getElementsByTagName()
returns a live HTMLCollection.
Array.from(collection)
collection.toArray()
collection.makeArray()
convertToArray(collection)
Answer: A) Array.from(collection)
Explanation: Array.from()
is the standard way to convert HTMLCollections to arrays.
element.getAttr()
element.getAttribute()
element.attributeValue()
element.fetchAttribute()
Answer: B) element.getAttribute()
Explanation: getAttribute()
is the standard method to retrieve the value of an attribute.
document.getElementById("myLink").href
document.getElementById("myLink").getAttribute("href")
Answer: C) Both A and B
Explanation: You can either access the property directly or use getAttribute()
to get the value.
setAttr()
changeAttribute()
setAttribute()
modifyAttr()
Answer: C) setAttribute()
Explanation: setAttribute()
lets you assign a new value to an attribute.
img.setAttribute("src", "new.jpg");
Answer: C) Changes the image source
Explanation: This sets a new value for the src
attribute of the image.
getAttribute()
return?
Answer: C) String
Explanation: All values returned by getAttribute()
are strings, even if numeric.
null
undefined
Answer: B) null
Explanation: getAttribute()
returns null
if the attribute doesn't exist.
input.setAttribute("type", "password");
Answer: A) Hides the input text
Explanation: Changing type to password
hides user input as dots.
p.addAttribute("title", "Info")
p.attribute = "title: Info"
p.setAttribute("title", "Info")
p.insert("title", "Info")
Answer: C) p.setAttribute("title", "Info")
Explanation: This is the correct syntax for adding a new attribute.
removeAttr()
deleteAttribute()
element.removeAttribute()
element.clearAttr()
Answer: C) element.removeAttribute()
Explanation: This method is used to completely remove an attribute.
alt
src
type
href
Answer: B) src
Explanation: The src
attribute specifies the image source path.
getAttributeNames()
getAllAttributes()
listAttributes()
getAttrList()
Answer: A) getAttributeNames()
Explanation: getAttributeNames()
returns an array of all attribute names on an element.
getAttributeNames()
return?
Answer: C) Array
Explanation: It returns an array of strings, where each string is an attribute name.
element.attributes
return?
Answer: C) A NamedNodeMap of attributes
Explanation: element.attributes
returns a NamedNodeMap, not a regular array.
element.attributes
?element.attributes[0].name
element.attributes.name[0]
element.getAttributeNames()[0]
element.getAttribute()[0].name
Answer: A) element.attributes[0].name
Explanation: Each attribute is an object with name
and value
properties.
element.attributes[0].value
?
Answer: C) The value of the attribute
Explanation: .value
retrieves the actual value assigned to the attribute.
element.attributes
?
Answer: D) NamedNodeMap
Explanation: It's a special map-like structure with attribute nodes, not a true array.
element.length
element.attributeCount
element.attributes.length
element.getAttributes().length
Answer: C) element.attributes.length
Explanation: attributes
behaves like an array-like object with a length property.
element.getAttributeNames().includes("class")
Answer: A) True if element has a class
Explanation: It checks if "class" is one of the element's attributes.
for (let attr of element.attributes)
for (let name of element.getAttributeNames())
Answer: C) Both A and B
Explanation: Both methods are valid for accessing all attribute names and values.
Answer: C) An attribute node
Explanation: Each item in attributes
is an attribute node with name
and value
.
document.newElement()
document.createElement()
document.generateElement()
document.makeElement()
Answer: B) document.createElement()
Explanation: The correct method to create a new element is createElement()
.
document.newTextNode()
document.createTextNode()
document.generateText()
document.writeTextNode()
Answer: B) document.createTextNode()
Explanation: createTextNode()
is used to generate a new text node.
appendChild()
addNode()
insertNode()
appendNode()
Answer: A) appendChild()
Explanation: appendChild()
adds a node to the end of a parent element.
let p = document.create("p"); document.body.add(p);
let p = document.makeElement("p"); document.appendChild(p);
let p = document.createElement("p"); document.body.appendChild(p);
let p = new Element("p"); document.append(p);
Answer: C) let p = document.createElement("p"); document.body.appendChild(p);
Explanation: This is the standard method to create and append a new paragraph element.
body
div
window
object
Answer: C) A newly created element
Explanation: The text node must be appended to a valid element node created via createElement()
.
appendChild()
return?
Answer: B) The added child node
Explanation: appendChild()
returns the node that was added.
Answer: C) It removes the node from the old position and appends it to the new
Explanation: A node can exist in only one place at a time in the DOM.
appendChild()
be used to add multiple elements at once?
Answer: B) No, it only accepts one node at a time
Explanation: To add multiple nodes, call appendChild()
repeatedly or use append()
method in modern JS.
let div = document.createElement("div");
div.innerHTML = "Hello";
document.body.appendChild(div);
Answer: A) A div with text "Hello" will be added to the page
Explanation: The div is created, assigned innerHTML, and appended to the body correctly.
appendChild()
addNode()
append()
write()
Answer: C) append()
Explanation: append()
allows appending multiple nodes or strings at once.
insertBefore()
addBefore()
insertNode()
appendBefore()
Answer: A) insertBefore()
Explanation: The insertBefore()
method inserts a new node before an existing child node.
insertBefore()
require as its arguments?
Answer: B) The new node and the position before which to insert
Explanation: The insertBefore()
method requires the new node and an existing child node as the reference for insertion.
insertChild()
insertBefore()
addBefore()
insertElement()
Answer: B) insertBefore()
Explanation: insertBefore()
is used to insert an element before another child in the DOM.
Answer: C) It appends the new node before the existing node
Explanation: The new node is inserted before the specified reference node.
insertBefore()
method to work?
Answer: C) Both the reference node and the new node must be specified
Explanation: You need to specify both the new node and the reference node for insertBefore()
to function correctly.
insertBefore()
to insert a new element?parentNode.insertBefore(newElement, null);
document.insertBefore(parentNode, newElement);
newElement.insertBefore(parentNode, nextSibling);
parentNode.insertBefore(newElement, nextSibling);
Answer: D) parentNode.insertBefore(newElement, nextSibling);
Explanation: The correct syntax to insert a new element before an existing one is insertBefore(newElement, nextSibling)
.
insertBefore()
insertChild()
appendChild()
append()
Answer: B) insertChild()
Explanation: insertChild()
is not a valid DOM method. The correct method is insertBefore()
.
insertBefore()
, what happens?
Answer: A) It inserts at the end of the parent node
Explanation: If no second argument is provided, the new node is appended to the parent element.
insertBefore()
?
Answer: C) It throws a "Node not found" error
Explanation: The reference node must be part of the DOM for insertBefore()
to work properly.
insertBefore()
be used to insert a new node as the first child of a parent node?null
as the second argumentnull
Answer: B) Yes, by passing the reference node as the first child
Explanation: To insert a new node as the first child, pass the first existing child node as the reference node.
Answer: B) It is a collection of key-value pairs
Explanation: In JavaScript, an object is a collection of key-value pairs, where the keys are unique identifiers for the values.
new Object();
{}
Object.create();
Answer: D) All of the above
Explanation: You can create a new object in JavaScript using new Object();
, {}
, or Object.create();
.
this
keyword refer to in an object?
Answer: C) The object itself
Explanation: The this
keyword refers to the object in which it is used, allowing access to the object's properties and methods.
object.property
object["property"]
Answer: C) Both A and B
Explanation: You can access the properties of an object using dot notation (object.property
) or bracket notation (object["property"]
).
Object.create()
methodnew Function()
Answer: D) Using new Function()
Explanation: The new Function()
method is used for creating functions, not objects.
console.log(typeof obj)
when obj
is an object?undefined
object
array
function
Answer: B) object
Explanation: In JavaScript, typeof
returns object
for objects, even though arrays and null are also considered objects.
name
and age
?let person = {name: "John", age: 30};
let person = {name = "John", age = 30};
let person = new Object(name: "John", age: 30);
let person = (name: "John", age: 30);
Answer: A) let person = {name: "John", age: 30};
Explanation: The correct syntax for defining an object with properties in JavaScript is to use object literal syntax like in option A.
Object.keys(obj)
return?
Answer: B) The names of the object's properties
Explanation: The Object.keys()
method returns an array of the names of an object's properties.
Object.addProperty()
Object.define()
Object.assign()
Answer: D) You can add properties directly by assigning them to the object
Explanation: You can add new properties to an object directly by assigning values to the new properties, like obj.newProperty = value
.
Object.freeze()
in JavaScript?
Answer: A) To prevent an object from being modified
Explanation: The Object.freeze()
method is used to freeze an object, preventing its properties from being added, deleted, or modified.
let car = {make: "Toyota", model: "Corolla"};
car.make = "Honda";
console.log(car.make);
Answer: B) "Honda"
Explanation: The value of car.make
is updated from "Toyota" to "Honda". When logged, it will display "Honda".
let user = {name: "Alice", age: 25};
let person = Object.create(user);
console.log(person.name);
Answer: A) "Alice"
Explanation: The person
object is created with user
as its prototype. The property name
is inherited from the prototype.
let obj = {x: 1};
Object.freeze(obj);
obj.x = 2;
console.log(obj.x);
Answer: A) 1
Explanation: After calling Object.freeze()
, the object obj
is immutable, meaning properties cannot be changed.
person
with the key city
and value "New York"?person.city = "New York";
Object.assign(person, {city: "New York"});
person["city"] = "New York";
Answer: D) All of the above
Explanation: All options are correct ways to add a property to an object in JavaScript.
age
from an object person
?delete person.age;
person.remove(age);
person.age = undefined;
remove.person(age);
Answer: A) delete person.age;
Explanation: The delete
operator is used to remove a property from an object in JavaScript.
Object.defineProperty()
do in JavaScript?
Answer: A) It defines a property of an object with specific attributes
Explanation: Object.defineProperty()
allows you to define a property and set its attributes (such as writable
, enumerable
, and configurable
).
let car = {make: "Toyota", model: "Corolla"};
Object.defineProperty(car, "year", {value: 2021, writable: false});
car.year = 2022;
console.log(car.year);
Answer: B) 2021
Explanation: The property year
is defined with writable: false
, so it cannot be modified. The value remains 2021.
let person = {name: "Alice"};
Object.defineProperty(person, "name", {writable: false});
person.name = "Bob";
console.log(person.name);
Answer: A) Alice
Explanation: Since the property name
is defined with writable: false
, it cannot be changed. The value remains "Alice".
dot
notation or bracket
notationget
methodset
methodapply
method
Answer: A) Using the dot
notation or bracket
notation
Explanation: In JavaScript, object properties can be accessed using dot notation (e.g., person.name
) or bracket notation (e.g., person["name"]
).
Object.defineProperty()
?writable: false
configurable: false
value
as the property valueenumerable: false
Answer: A) Set writable: false
Explanation: To make a property read-only, you need to set the writable
attribute to false
using Object.defineProperty()
.
let obj = {};
Object.defineProperty(obj, "name", {value: "Alice", writable: false});
obj.name = "Bob";
console.log(obj.name);
Answer: A) Alice
Explanation: Since name
is defined as writable: false
, the value cannot be changed and it remains "Alice".
Object.freeze()
Object.seal()
Object.defineProperty()
with writable: true
Object.preventExtensions()
Answer: C) Using Object.defineProperty()
with writable: true
Explanation: If a property is defined with writable: true
using Object.defineProperty()
, it can be changed.
let student = {name: "John"};
Object.defineProperty(student, "name", {value: "Doe", writable: false});
console.log(student.name);
Answer: B) Doe
Explanation: The value of name
is initially set to "Doe" using Object.defineProperty()
, but the property is not writable so it cannot be changed afterward.
enumerable: true
in Object.defineProperty()
configurable: false
Object.freeze()
Object.preventExtensions()
Answer: A) Set enumerable: true
in Object.defineProperty()
Explanation: In Object.defineProperty()
, setting enumerable: true
makes the property enumerable, meaning it will show up in for...in
loops.
configurable: false
?
Answer: D) All of the above
Explanation: When a property is defined with configurable: false
, it cannot be deleted, its attributes cannot be modified, and it cannot be redefined.