JavaScript in the Browser: Forms, Storage, and Cookies Quiz Quiz

  1. Form Validation Basics

    Which JavaScript method can be used to prevent a form from submitting if certain fields are invalid?

    1. preventFormAction()
    2. event.blockDefault()
    3. event.preventDefault()
    4. stopSubmission()
    5. form.cancelSubmit()
  2. Accessing Local Storage

    What method would you use to store a value 'hello' under the key 'greeting' in the browser's local storage?

    1. localStorage.setItem('greeting', 'hello')
    2. localStorage.addItem('greeting', 'hello')
    3. localStorage.save('greeting', 'hello')
    4. sessionStorage.setValue('greeting', 'hello')
    5. localStorage.put('greeting', 'hello')
  3. Getting Input Value

    Given an input field with id='user', how can you retrieve its value with JavaScript?

    1. document.input('user').value
    2. document.getInputById('user').val()
    3. document.value('user').get()
    4. document.getElementById('user').value
    5. getElementById('user').content
  4. Checking Local Storage Support

    Which JavaScript feature should you check to confirm if the browser supports local storage?

    1. window.sessionCookies
    2. browser.localStorageSupport
    3. localStorageEnabled
    4. window.localStorage
    5. window.hasLocalData
  5. Reading a Cookie Value

    How do you access all cookies set for the current page in JavaScript?

    1. window.cookies
    2. browser.cookies
    3. document.cookie
    4. navigator.cookie
    5. cookie.document
  6. Session Storage Lifetime

    What happens to data stored with sessionStorage after you close the browser tab?

    1. It is saved permanently
    2. It moves to cookies
    3. It is transferred to localStorage
    4. It is deleted automatically
    5. It is sent to the server
  7. HTML Form Attribute

    Which HTML attribute can help trigger the browser's built-in form validation for required fields?

    1. fillOut
    2. mustFill
    3. required
    4. validateInput
    5. mandatory
  8. Cookie Expiration

    Which cookie attribute specifies when a cookie will be removed automatically?

    1. expires
    2. lifetime
    3. removeAfter
    4. expiryTime
    5. timeout
  9. Session Storage Key Retrieval

    What method allows you to retrieve the value of a key named 'username' from sessionStorage?

    1. sessionStorage.find('username')
    2. sessionStorage.getItem('username')
    3. sessionStorage.valueOf('username')
    4. sessionStorage.retrieve('username')
    5. sessionStorage.get('username')
  10. Removing From Local Storage

    If you want to delete the item with key 'token' from local storage, which method should you use?

    1. localStorage.remove('token')
    2. localStorage.removeItem('token')
    3. localStorage.delete('token')
    4. localStorage.clearItem('token')
    5. localStorage.forget('token')
  11. Custom Pattern Validation

    What HTML attribute can be used to specify a regular expression for input validation?

    1. validate
    2. expression
    3. regex
    4. pattern
    5. match
  12. Cookie Path Attribute

    Which attribute in a cookie determines which URLs on the domain the cookie is accessible to?

    1. location
    2. urlLimit
    3. scope
    4. area
    5. path
  13. Clearing All Local Storage

    What method removes all key-value pairs from the browser's local storage at once?

    1. localStorage.purge()
    2. localStorage.erase()
    3. localStorage.clear()
    4. localStorage.removeAll()
    5. localStorage.empty()
  14. Difference Between Local and Session Storage

    What is a key difference between localStorage and sessionStorage in browsers?

    1. localStorage cannot store strings
    2. sessionStorage has larger storage capacity
    3. sessionStorage is visible to all browser tabs; localStorage is not
    4. localStorage is faster than sessionStorage
    5. localStorage persists after closing the browser; sessionStorage does not
  15. Setting a Cookie in JavaScript

    How do you set a cookie named 'auth' with value 'yes' using JavaScript?

    1. setCookie('auth','yes')
    2. cookie.set('auth','yes')
    3. document.cookie = 'auth=yes';
    4. window.cookie = 'auth:yes'
    5. document.setCookie('auth','yes')