[{"id":"javascriptbeginner-001","courseId":"javascript-beginner","domain":"values-and-types","topic":"Primitive value types","prompt":"A learner reads that everything in JavaScript is an object. They write const city = 'Lisbon'; and call city.toUpperCase() without error. Which statement about city is correct?","hint":"Ask what kind of data a string is, separate from which methods you can call on it.","explain":"JavaScript values are either primitives or objects. Primitives are plain data that cannot be changed in place, while objects are containers that hold their own properties.","source":"https://developer.mozilla.org/en-US/docs/Glossary/Primitive","interaction":{"type":"single"},"options":[{"id":"a","text":"city holds a primitive string, and it has no methods or properties of its own.","why":"A string is a primitive. Calls like toUpperCase() still work because JavaScript wraps the value temporarily for the call."},{"id":"b","text":"city is an object, because it can call methods like toUpperCase().","whyNot":"Being able to call a method does not make a value an object; primitives are not objects even though methods work on them."},{"id":"c","text":"city is a primitive that can gain a note property with city.note = 'x'.","whyNot":"Assigning a property to a primitive writes to a temporary wrapper, so the string itself never gets a note property."},{"id":"d","text":"city is a reference to a String object stored in the variable.","whyNot":"The variable holds the string value itself; a String object exists only briefly while a method call runs."}],"answer":"a","explanation":"A primitive is data that is not an object and has no methods or properties. Calling a method on one works because JavaScript temporarily wraps it in an object, and that wrapper is discarded.","concept":"A primitive is data that is not an object and has no methods or properties. Calling a method on one works because JavaScript temporarily wraps it in an object, and that wrapper is discarded.","rev":1,"kind":"scenario","code":"const city = 'Lisbon';\nconsole.log(city.toUpperCase());","takeaways":["Strings, numbers and booleans are primitives; methods on them come from a temporary wrapper."]},{"id":"javascriptbeginner-002","courseId":"javascript-beginner","domain":"values-and-types","topic":"Truthy and falsy values","prompt":"A form saves an optional count and nickname. Which TWO of these values are falsy, so an if (value) check skips them?","hint":"Ask which values an if test turns into false.","explain":"An if test converts its value to a boolean. Only a short, fixed list of values converts to false, and every other value converts to true.","source":"https://developer.mozilla.org/en-US/docs/Glossary/Falsy","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"'' (the empty string)","why":"The empty string is one of the falsy values, so an if test skips it."},{"id":"b","text":"0 (the number zero)","why":"The number zero is falsy, so a count of 0 fails an if test."},{"id":"c","text":"'0' (a string holding the character zero)","whyNot":"Any non-empty string is truthy, so the text '0' passes the test."},{"id":"d","text":"[] (an empty array)","whyNot":"An array is an object, and the only falsy object is document.all; an empty array is truthy."},{"id":"e","text":"' ' (a string holding one space)","whyNot":"A space is a character, so the string is not empty and counts as truthy."},{"id":"f","text":"'false' (the four-letter text)","whyNot":"Only the keyword false is falsy; the text is non-empty and therefore truthy."}],"answer":"ab","explanation":"The falsy values are false, null, undefined, NaN, 0, -0, 0n, the empty string and document.all. Other values, including non-empty strings and empty arrays, are truthy.","concept":"The falsy values are false, null, undefined, NaN, 0, -0, 0n, the empty string and document.all. Other values, including non-empty strings and empty arrays, are truthy.","rev":1,"kind":"scenario","takeaways":["Zero and the empty string are falsy, but the text '0' and an empty array are truthy."]},{"id":"javascriptbeginner-003","courseId":"javascript-beginner","domain":"values-and-types","topic":"Strict equality (===)","prompt":"The strict equality operator (===) applies a fixed set of checks to two operands. Put these checks in the order the operator applies them.","hint":"Some checks can settle the answer without reading the values at all.","explain":"The strict equality operator never converts one operand to match the other. It follows a fixed series of rules that treat types, objects, null, undefined and NaN in their own ways.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality","interaction":{"type":"sort","items":[{"text":"If the operands have different types, return false.","why":"Type is compared first, so mixed types such as 1 and '1' are settled before any value is read."},{"text":"If both operands are objects, return true only if they are the same object.","why":"This applies once the types match, since both operands are objects, and it compares identity rather than contents."},{"text":"If both are null, or both are undefined, return true.","why":"These pairs are decided by their type alone, before any numeric or text comparison happens."},{"text":"If either operand is NaN, return false.","why":"NaN is caught before plain values are compared, because it never equals anything, itself included."},{"text":"Otherwise, compare the two values themselves.","why":"This is the fallback, reached only when none of the earlier rules has already decided the result."}]},"options":[],"answer":"o:abcde","explanation":"=== returns false for different types, compares objects by identity, returns true for null with null or undefined with undefined, returns false for NaN, and otherwise compares values.","concept":"=== returns false for different types, compares objects by identity, returns true for null with null or undefined with undefined, returns false for NaN, and otherwise compares values.","rev":1,"kind":"scenario","takeaways":["=== never converts types, so 1 === '1' is false."]},{"id":"javascriptbeginner-004","courseId":"javascript-beginner","domain":"values-and-types","topic":"The undefined value","prompt":"Given this code in one block, the first line logs 'undefined' without throwing an error.","hint":"Think about a let variable before the line that declares it.","explain":"A let variable exists from the top of its block, but it cannot be read until its declaration runs. That early window is called the temporal dead zone.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"typeof is only forgiving about names that were never declared; a let name inside its dead zone still throws a ReferenceError."},{"id":"b","text":"False","why":"Even with typeof, reading total before its declaration throws a ReferenceError, so the first line logs nothing."}],"answer":"b","explanation":"typeof is safe for names that were never declared, but a let or const name is in its temporal dead zone until its declaration runs, so typeof on it throws a ReferenceError.","concept":"typeof is safe for names that were never declared, but a let or const name is in its temporal dead zone until its declaration runs, so typeof on it throws a ReferenceError.","rev":1,"kind":"scenario","code":"console.log(typeof total);\nlet total = 5;","takeaways":["typeof avoids errors only for undeclared names; let and const names still throw before their declaration."]},{"id":"javascriptbeginner-005","courseId":"javascript-beginner","domain":"values-and-types","topic":"NaN comparisons","prompt":"A script checks whether a calculation produced NaN with result === NaN, but the failure branch never runs. Which check reliably detects NaN?","hint":"Think about how NaN behaves when compared with itself.","explain":"NaN stands for 'not a number', and it is the only value that is not equal to itself. Ordinary equality checks therefore never succeed for it, so a dedicated test is needed.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN","interaction":{"type":"single"},"options":[{"id":"a","text":"Number.isNaN(result)","why":"Number.isNaN returns true only when the value is NaN, so it detects the failure without comparing against NaN."},{"id":"b","text":"result === NaN","whyNot":"NaN is not equal to anything, including itself, so this comparison is always false."},{"id":"c","text":"result === undefined","whyNot":"undefined is a different value; a failed calculation produces NaN, which is not a missing value."},{"id":"d","text":"result === 'NaN'","whyNot":"The text 'NaN' is a string, and strict equality returns false when the types differ."}],"answer":"a","explanation":"NaN is not equal to anything, including itself, so NaN === NaN is false. Number.isNaN() is the reliable test because it returns true only for NaN.","concept":"NaN is not equal to anything, including itself, so NaN === NaN is false. Number.isNaN() is the reliable test because it returns true only for NaN.","rev":1,"kind":"scenario","takeaways":["Never test for NaN with === or ==; use Number.isNaN()."]}]