Introduction To Javascript Answers
Introduction To Javascript Answers
A. To create a constant
B. To declare a variable
C. To define a function
D. To create a block-scoped variable
Answer: B
Explanation: The var keyword is used to declare a variable, but it has function scope, not
block scope (use let or const for block scope).
Answer: C
Explanation: let is used to declare variables with block-level scope, meaning they are limited
to the block in which they are defined (e.g., inside a loop or an if statement).
Answer: B
Explanation: The const keyword is used to declare constants. These cannot be reassigned
once initialized, but note that objects declared with const can still be mutated.
Answer: A
Explanation: The syntax let obj = {}; correctly initializes an empty object in JavaScript.
A. object.addProperty('key', value)
B. object['key'] = value
C. object.add('key', value)
D. object.set('key', value)
Answer: B
Explanation: You can add a new property to an object using bracket notation like
object['key'] = value.
Answer: C
Explanation: The this keyword refers to the object that is executing the current function,
depending on the context in which the function is called.
A. function myFunction() {}
B. function = myFunction() {}
C. def myFunction() {}
D. function: myFunction() {}
Answer: A
Explanation: The correct way to define a function in JavaScript is function myFunction()
{}.
Answer: A
Explanation: The return statement stops the execution of a function and sends a specified
value back to the calling code.
A. forEach()
B. loop()
C. repeat()
D. iterate()
Answer: A
Explanation: forEach() is an array method in JavaScript that allows you to iterate over the
elements of an array.
A. "null"
B. "object"
C. "undefined"
D. "array"
Answer: B
Explanation: The typeof operator returns "object" for null, which is a quirk in JavaScript
that can lead to confusion.
11. How would you create an array in JavaScript?
Answer: B
Explanation: Arrays are created in JavaScript using square brackets like let arr = [1, 2,
3];.
12. Which method would you use to convert a string to an integer in JavaScript?
A. parseInt()
B. toInteger()
C. convertToInt()
D. stringToInt()
Answer: A
Explanation: The parseInt() function is used to convert a string to an integer in JavaScript.
Answer: A
Explanation: JSON.stringify() is used to convert a JavaScript object into a JSON string.
A. ==
B. ===
C. =
D. !=
Answer: B
Explanation: The === operator checks for strict equality, meaning it compares both the value
and the type.
Answer: B
Explanation: A closure is a function that has access to its own scope, the scope in which it was
created, and the global scope.
Answer: B
Explanation: The map() method creates a new array with the results of calling a provided
function on every element in the calling array.
Answer: B
Explanation: const is used to declare constants, so attempting to reassign a value to a const
variable will result in an error.
A. catch block
B. try...catch statement
C. error() function
D. handleError() function
Answer: B
Explanation: Errors in JavaScript are handled using try...catch blocks, where code that
may throw an error is placed in the try block, and error handling is done in the catch block.
20. Which of the following methods is used to remove the last element of an array in
JavaScript?
A. pop()
B. shift()
C. unshift()
D. splice()
Answer: A
Explanation: The pop() method removes the last element from an array and returns that
element.
A. class MyClass {}
B. class = MyClass {}
C. class MyClass[]
D. function class MyClass {}
Answer: A
Explanation: The correct syntax for defining a class in JavaScript is class MyClass {}.
A. setTimeout()
B. setInterval()
C. async/await
D. all()
Answer: C
Explanation: async/await is used to handle asynchronous operations in JavaScript, allowing
you to write asynchronous code in a synchronous-like manner.
Answer: B
Explanation: null is an object, whereas undefined represents the absence of a value or a
variable that has not been assigned a value.
24. Which event is used to trigger an action when an element is clicked in JavaScript?
A. onmouseover
B. onkeydown
C. onclick
D. onload
Answer: C
Explanation: The onclick event is triggered when an element is clicked.
25. What does JSON.parse() do in JavaScript?
Answer: B
Explanation: JSON.parse() is used to convert a valid JSON string into a JavaScript object.
A. "number"
B. "NaN"
C. "undefined"
D. "object"
Answer: A
Explanation: NaN stands for "Not-A-Number," but it is still considered a type of number in
JavaScript.
A. form.preventDefault();
B. event.preventDefault();
C. form.stopSubmit();
D. event.stopSubmit();
Answer: B
Explanation: event.preventDefault() is used to prevent the default action of an event,
such as form submission.
A. +
B. &
C. concat()
D. +=
Answer: A
Explanation: The + operator is used to concatenate strings in JavaScript.
29. Which method can be used to check if an object contains a specific property in
JavaScript?
A. object.contains(key)
B. object.hasOwnProperty(key)
C. object.include(key)
D. object.getProperty(key)
Answer: B
Explanation: The method hasOwnProperty(key) is used to check if an object has a specific
property.
A. true
B. false
C. undefined
D. null
Answer: A
Explanation: This expression evaluates to true due to type coercion in JavaScript. An empty
array [] is loosely equal to false, and ![] evaluates to false.
A. true
B. false
C. undefined
D. NaN
Answer: B
Explanation: Due to floating-point precision issues in JavaScript, 0.1 + 0.2 does not exactly
equal 0.3, so the result is false.
33. Which of the following is used to add an event listener to an element in JavaScript?
A. element.addEventListener()
B. element.onEvent()
C. element.bindEvent()
D. element.listenEvent()
Answer: A
Explanation: addEventListener() is used to attach an event handler to a DOM element in
JavaScript.
A. array.copy()
B. array.clone()
C. array.slice()
D. array.duplicate()
Answer: C
Explanation: The slice() method can be used to create a shallow copy of an array.
35. Which of the following is the correct way to declare a class in JavaScript?
A. class MyClass {}
B. new class MyClass {}
C. class = MyClass {}
D. class MyClass[] {}
Answer: A
Explanation: The correct syntax for declaring a class in JavaScript is class MyClass {}.
A. function myFunction() {}
B. def myFunction() {}
C. func myFunction() {}
D. declare function myFunction() {}
Answer: A
Explanation: Functions in JavaScript are declared using the function keyword followed by
the function name and parentheses.
Answer: B
Explanation: The bind() method creates a new function that, when invoked, has its this
value set to the provided value, with a given sequence of arguments.
A. 11
B. 2
C. 1
D. NaN
Answer: A
Explanation: In JavaScript, the + operator will convert 1 to a string and concatenate it with "1",
resulting in "11".
A. string.toLowerCase()
B. string.lowerCase()
C. string.makeLower()
D. string.toLower()
Answer: A
Explanation: The toLowerCase() method converts a string to lowercase in JavaScript.
A. true
B. false
C. undefined
D. null
Answer: A
Explanation: An empty array [] is loosely equal to false due to type coercion in JavaScript.
Answer: B
Explanation: setInterval() is used to repeatedly execute a function at fixed intervals,
specified in milliseconds.
A. clearInterval()
B. stopInterval()
C. cancelInterval()
D. endInterval()
Answer: A
Explanation: The clearInterval() method is used to stop a function from executing
repeatedly after being set with setInterval().
A. 10
B. 5
C. 15
D. NaN
Answer: B
Explanation: The - operator in JavaScript triggers type coercion, converting the string "10" to
the number 10 and then subtracting 5, resulting in 5.
45. How can you declare a JavaScript variable that is globally scoped inside a function?
46. Which method is used to search for a substring within a string in JavaScript?
A. indexOf()
B. substring()
C. search()
D. includes()
Answer: A
Explanation: The indexOf() method returns the index of the first occurrence of a specified
substring in a string.
A. Deletes a variable
B. Deletes a property from an object
C. Deletes an element from an array
D. Deletes a function
Answer: B
Explanation: The delete operator is used to remove a property from an object, but it does not
affect variables or array elements directly.
48. What is the default value of a JavaScript variable that is declared but not assigned a
value?
A. 0
B. null
C. undefined
D. NaN
Answer: C
Explanation: In JavaScript, variables that are declared but not initialized have the default value
undefined.
49. Which of the following is an example of a falsy value in JavaScript?
A. "hello"
B. 1
C. true
D. "" (empty string)
Answer: D
Explanation: The empty string "" is a falsy value in JavaScript, meaning it evaluates to false
in a boolean context.
50. Which of the following methods is used to parse a query string into an object in
JavaScript?
A. URLSearchParams
B. queryParser()
C. parseQueryString()
D. getQueryParams()
Answer: A
Explanation: URLSearchParams is used to parse query strings into an object that can be
easily accessed and manipulated.