JavaScript Basic Exercises


1. What is JavaScript?
  1. Programming Language
  2. Scripting Language
  3. Both Programming and Scripting Language
  4. None of the above
Answer 3. Both Programming and Scripting Language

2. What does NaN represent in JavaScript?
  1. Not A Name
  2. Not A Number
  3. Null And Number
  4. Number And Null
Answer 2. Not A Number

3. Which company developed JavaScript?
  1. Microsoft
  2. Apple
  3. Netscape
  4. Google
Answer 3. Netscape

4. Which of the following is a correct way to write a comment in JavaScript?
  1. <!-- This is a comment -->
  2. /* This is a comment */
  3. # This is a comment
  4. $ This is a comment
Answer 2. /* This is a comment */

5. What is the result of the expression 2 + "2" in JavaScript?
  1. 4
  2. “22”
  3. NaN
  4. Undefined
Answer 2. "22"

6. How do you declare a variable in JavaScript?
  1. let
  2. var
  3. const
  4. All of the above
Answer 4. All of the above

7. What is the correct syntax for a for loop in JavaScript?
  1. for (let i = 0; i < 5; i++)
  2. for i in 5
  3. foreach (i in range(5))
  4. 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);
  1. null
  2. object
  3. undefined
  4. string
Answer 2. `object`

9. How do you create an array in JavaScript?
  1. var arr = []
  2. var arr = array()
  3. var arr = new Array()
  4. Both 1 and 3
Answer 4. Both 1 and 3

10. What is the use of === in JavaScript?
  1. Compares both value and type
  2. Compares value only
  3. Compares type only
  4. None of the above
Answer 1. Compares both value and type

11. What is the purpose of isNaN function in JavaScript?
  1. Checks if the value is a number
  2. Converts a value to a number
  3. Checks if the value is NaN (Not-A-Number)
  4. 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. 1
  2. 2
  3. true
  4. NaN
Answer 2. `2`

13. Which method is used to parse a string to an integer in JavaScript?
  1. Number.parse()
  2. parseInt()
  3. parseFloat()
  4. toString()
Answer 2. `parseInt()`

14. How do you write a function in JavaScript?
  1. def myFunction()
  2. function myFunction()
  3. func myFunction()
  4. function:myFunction()
Answer 2. `function myFunction()`

15. Which of the following is a JavaScript framework?
  1. Angular
  2. Django
  3. Laravel
  4. Flask
Answer 1. Angular

16. Which of the following will stop the execution of a JavaScript function?
  1. break
  2. continue
  3. return
  4. exit
Answer 3. `return`

17. How do you call a function named myFunction in JavaScript?
  1. call myFunction()
  2. myFunction()
  3. myFunction
  4. execute myFunction()
Answer 2. `myFunction()`

18. How do you check if two variables a and b are not equal in JavaScript?
  1. a != b
  2. a !== b
  3. a <> b
  4. Both 1 and 2
Answer 4. Both 1 and 2

19. What does null mean in JavaScript?
  1. A value that represents nothing
  2. A value that represents zero
  3. A value that represents undefined
  4. 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?
  1. getElementById()
  2. querySelector()
  3. getElementByClass()
  4. getElementsByTag()
Answer 1. `getElementById()`

21. How do you round a number to the nearest integer in JavaScript?
  1. Math.round()
  2. Math.floor()
  3. Math.ceil()
  4. Math.random()
Answer 1. `Math.round()`

22. What does the push() method do in JavaScript?
  1. Removes the first element from an array
  2. Adds an element to the end of an array
  3. Removes the last element from an array
  4. 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?
  1. JSON.stringify()
  2. JSON.parse()
  3. toString()
  4. parseJSON()
Answer 1. `JSON.stringify()`

24. What is the default value of an uninitialized variable in JavaScript?
  1. null
  2. 0
  3. undefined
  4. NaN
Answer 3. `undefined`

25. What is the difference between let and var in JavaScript?
  1. let has block scope while var has function scope
  2. let has function scope while var has block scope
  3. There is no difference
  4. let cannot be reassigned while var can
Answer 1. `let` has block scope while `var` has function scope