32. JavaScript Quiz
Test yourself
Choose an answer for each question. You get feedback straight away, and your best score is saved on this device.
Which HTML element holds JavaScript code?
<js><code><script><javascript>
The <script> element holds JavaScript, either inline or linked with src.
Which keyword declares a value that cannot be reassigned?
letconstvarstatic
const blocks reassignment. Use it by default, and let only when the value must change.
What does "5" + 2 produce?
7NaN- An error
"52"
When one side is text, + joins instead of adding, so the result is the string “52”.
Which operator compares both value and type?
=======>
=== is strict equality. = assigns, and => is for arrow functions.
What is the index of the first item in an array?
- 1
- 0
- -1
- It depends on the array
Array positions start at 0, so the last item is at length - 1.
Which array method returns a new array containing only the items that pass a test?
map()reduce()filter()find()
filter keeps matching items. map transforms every item, and reduce combines them into one value.
What does a function return if it has no return statement?
undefinednull0- An empty string
A function without return gives back undefined.
Which is the safest way to put text typed by a visitor into the page?
element.innerHTML = textdocument.write(text)eval(text)element.textContent = text
textContent treats the text as plain text, so it cannot run HTML or script (XSS).
Which method returns only the first element matching a CSS selector?
querySelectorAll()querySelector()getElementsByTagName()findElement()
querySelector returns one element or null. querySelectorAll returns a list.
What does event.preventDefault() do on a form’s submit event?
- Deletes the form
- Checks the form is valid
- Stops the browser’s normal submit and page reload
- Sends the form twice
It stops the browser’s default action so your own code can handle the form.
Which method turns an object into JSON text?
JSON.stringify()JSON.parse()JSON.toObject()Object.toJSON()
stringify makes text from an object, and parse makes an object from text.
A fetch() request returns a 404 response. What happens to the promise?
- It is rejected automatically
- It still resolves, so you must check
response.ok - The page reloads
- It retries three times
fetch rejects only on network failure. HTTP errors such as 404 still resolve, so check response.ok.
What next?
You have finished the JavaScript course. Continue with the PHP Tutorial to learn how websites work on the server side.