JavaScript Basic Exercises
1. What is JavaScript?
- Programming Language
- Scripting Language
- Both Programming and Scripting Language
- None of the above
Answer
3. Both Programming and Scripting Language
2. What does NaN represent in JavaScript?
- Not A Name
- Not A Number
- Null And Number
- Number And Null
Answer
2. Not A Number
3. Which company developed JavaScript?
- Microsoft
- Apple
- Netscape
- Google
Answer
3. Netscape
<!-- This is a comment -->
/* This is a comment */
# This is a comment
$ This is a comment
Answer
2. /* This is a comment */
5. What is the result of the expression 2 + "2" in JavaScript?
- 4
- “22”
- NaN
- Undefined
Answer
2. "22"
6. How do you declare a variable in JavaScript?
let
var
const
- All of the above
Answer
4. All of the above
7. What is the correct syntax for a for loop in JavaScript?
for (let i = 0; i < 5; i++)
for i in 5
foreach (i in range(5))
for (i <= 5; i++)
Answer
1. `for (let i = 0; i < 5; i++)`
8. What is the output of the following code:
console.log(typeof null);
null
object
undefined
string
Answer
2. `object`
9. How do you create an array in JavaScript?
var arr = []
var arr = array()
var arr = new Array()
- Both 1 and 3
Answer
4. Both 1 and 3
10. What is the use of === in JavaScript?
- Compares both value and type
- Compares value only
- Compares type only
- None of the above
Answer
1. Compares both value and type
11. What is the purpose of isNaN function in JavaScript?
- Checks if the value is a number
- Converts a value to a number
- Checks if the value is NaN (Not-A-Number)
- None of the above
Answer
3. Checks if the value is NaN (Not-A-Number)
12. What is the result of true + true in JavaScript?
1
2
true
NaN
Answer
2. `2`
13. Which method is used to parse a string to an integer in JavaScript?
Number.parse()
parseInt()
parseFloat()
toString()
Answer
2. `parseInt()`
14. How do you write a function in JavaScript?
def myFunction()
function myFunction()
func myFunction()
function:myFunction()
Answer
2. `function myFunction()`
15. Which of the following is a JavaScript framework?
- Angular
- Django
- Laravel
- Flask
Answer
1. Angular
16. Which of the following will stop the execution of a JavaScript function?
break
continue
return
exit
Answer
3. `return`
17. How do you call a function named myFunction in JavaScript?
call myFunction()
myFunction()
myFunction
execute myFunction()
Answer
2. `myFunction()`
18. How do you check if two variables a and b are not equal in JavaScript?
a != b
a !== b
a <> b
- Both 1 and 2
Answer
4. Both 1 and 2
19. What does null mean in JavaScript?
- A value that represents nothing
- A value that represents zero
- A value that represents undefined
- A variable that has not been assigned a value
Answer
1. A value that represents nothing
20. Which method can be used to select an HTML element by its ID in JavaScript?
getElementById()
querySelector()
getElementByClass()
getElementsByTag()
Answer
1. `getElementById()`
21. How do you round a number to the nearest integer in JavaScript?
Math.round()
Math.floor()
Math.ceil()
Math.random()
Answer
1. `Math.round()`
22. What does the push() method do in JavaScript?
- Removes the first element from an array
- Adds an element to the end of an array
- Removes the last element from an array
- Adds an element to the beginning of an array
Answer
2. Adds an element to the end of an array
23. Which method is used to convert a JavaScript object to a JSON string?
JSON.stringify()
JSON.parse()
toString()
parseJSON()
Answer
1. `JSON.stringify()`
24. What is the default value of an uninitialized variable in JavaScript?
null
0
undefined
NaN
Answer
3. `undefined`
25. What is the difference between let and var in JavaScript?
let has block scope while var has function scope
let has function scope while var has block scope
- There is no difference
let cannot be reassigned while var can
Answer
1. `let` has block scope while `var` has function scope