JS-Dev-101 practice question 1 of 25
A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the behavior?
Full set: 148 questionsThe document.cookie property is an accessor, not a simple data property. When you read it, you get a string of all cookies for the document. When you assign a value to it, you are not overwriting all cookies. Instead, the browser parses the string and adds the new cookie or updates an existing cookie with the same name. All other cookies remain unaffected by this operation. The value 'John Smith' is valid, as modern browsers will handle the space.
B. The assignment will typically succeed. While URL encoding is best practice, browsers are lenient with spaces in cookie values and will often handle them correctly.
C. The property is document.cookie (singular), not cookies. The assignment operation adds or updates a single cookie; it does not wipe existing ones.
D. Assigning to document.cookie does not wipe or overwrite all other existing cookies; it only adds or updates the specified cookie.
1. MDN Web Docs, Document.cookie, "Using document.cookie" section. States: "You can set a new cookie by assigning a string with the cookie value to document.cookie... Note that this will not remove or change other cookies that are already stored."
2. Salesforce Lightning Web Components Dev Guide, Communicate with Cookies, "Set a Cookie" section. Demonstrates the same additive behavior of assigning to document.cookie.
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 401 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
- Question 7Given a value, which two options can a developer use to detect if the value is NaN?
