General JS I
1.
How do you properly check if a variable is undefined in JavaScript?
var x;
if (x == null)
if (typeof x === 'undefined')
if (x === undefined)
2.
What will be logged to the console in the following code?
var x = 1; function foo() { console.log(x); var x = 2; } foo();
2
1
undefined
ReferenceError
3.
What is the purpose of the `this` keyword in JavaScript?
Refers to the previous object
Refers to the current object
Refers to the parent object
Refers to the global object
4.
Which of the following is NOT a valid way to declare a JavaScript variable?
var x = 10;
const y = 20;
let z = 30;
int w = 40;
5.
What is the purpose of the `event.preventDefault()` method in JavaScript?
Stops the event from bubbling up the DOM tree
Prevents the default action of the event
Stops the event propagation to the parent elements
Disables the event completely
6.
How do you check if an object has a specific property in JavaScript?
if (myObj.property)
if (myObj.property === undefined)
if (myObj.hasOwnProperty('property'))
if ('property' in myObj)
7.
What is the purpose of the `typeof` operator in JavaScript?
Checks if a variable is defined
Returns the data type of a variable
Converts a variable to a specific type
Checks if a variable is null
8.
What is the difference between `==` and `===` in JavaScript?
Both are strict equality operators
`==` is strict, and `===` is loose
`==` is loose, and `===` is strict
They are completely interchangeable
9.
How do you declare a function in JavaScript?
var myFunction = function(){};
function myFunction(){}
both are correct
None of the above
10.
What is the result of the expression `0 == false`?
null
FALSE
undefined
TRUE
11.
Which of the following is a valid way to comment in JavaScript?
/* This is a comment */
// This is a comment
# This is a comment
-- This is a comment
12.
What is the purpose of the `JSON.parse()` method in JavaScript?
Parses a string and returns an integer
Converts a JavaScript object to a JSON string
Converts a JSON string to a JavaScript object
Encodes a JavaScript object into JSON format
13.
How would you round the number 7.25 to the nearest integer in JavaScript?
round(7.25)
Math.floor(7.25)
Math.ceil(7.25)
Math.round(7.25)
14.
Which event is triggered when a user clicks on an HTML element in JavaScript?
onmouseover
onmousedown
onclick
onchange
15.
How can you check the length of an array in JavaScript?
array.size()
array.count()
array.length()
array.size
16.
What is the purpose of the `setTimeout` function in JavaScript?
Sets a timer for a specific time
Delays the execution of a function
Repeats a function at regular intervals
Clears the timer set by `setInterval`
17.
What is the purpose of the `querySelector` method in JavaScript?
Selects all elements that match a specified CSS selector
Selects the first element that matches a specified CSS selector
Selects the parent element of a specified child element
Selects elements based on their tag name
18.
How do you declare a constant variable in JavaScript?
let x = 10;
var x = 10;
const x = 10;
Both `const` and `let`
19.
What does the following JavaScript code output?
console.log(2 + '2');
4'
22
NaN
22'
20.
What is the result of the expression `3 == '3'`?
undefined
FALSE
TRUE
null