Davinci on Nostr: {"questionMarkdown":"Suppose you are writing code to take in a user's favorite fruit ...
{"questionMarkdown":"Suppose you are writing code to take in a user's favorite fruit and output it. During prototyping, you write some basic code, but accidentally switch the order of statements:\r\n\r\n```\r\n// ... some code\r\nconsole.log(fruit);\r\nvar fruit = \"bananas\";\r\n// ... some more code\r\n```\r\n\r\nDuring runtime, what will be the output of this segment?","explanationMarkdown":"The correct answer is:\r\n> undefined\r\n\r\nThe phenomenon at play is one of Javascript's stranger quirks, called hoisting.\r\n\r\nBasically, any statement involving the `var` keyword gets \"hoisted\" to the top of its scope. So, even though we use the `fruit` variable before we delcare it, the statement that initializes `fruit` runs before we print the content of `fruit`.\r\n\r\nHowever, this does not mean that the code will print `bananas` correctly! Javascript only hoists initializations, not declarations. In other words, the code is equivalent to the following:\r\n\r\n```\r\nvar fruit;\r\nconsole.log(fruit);\r\nfruit = \"bananas\";\r\n```\r\n\r\nWhen a variable like `fruit` is initialized without a value, it is `undefined` (as opposed to `null`). Thus, the code will not throw an error, and print `undefined`.\r\n\r\nFor more on hoisting, [MDN's page](
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting).","answers":[{"isCorrect":false,"answerMarkdown":"null"},{"isCorrect":false,"answerMarkdown":"Uncaught ReferenceError: y is not defined"},{"isCorrect":false,"answerMarkdown":"bananas"},{"isCorrect":true,"answerMarkdown":"undefined"}],"topics":["vanilla-js"]}
Published at
2023-08-23 15:39:56Event JSON
{
"id": "3748a51225f4f8fc7b802924e0568ea9497fc1e0638d1dd994da027d6cfa1ed6",
"pubkey": "02593ef15c27d3f61f2e208692f8dde7894b0a28a84c870c2e0d5215fcc7ad85",
"created_at": 1692805196,
"kind": 1,
"tags": [],
"content": "{\"questionMarkdown\":\"Suppose you are writing code to take in a user's favorite fruit and output it. During prototyping, you write some basic code, but accidentally switch the order of statements:\\r\\n\\r\\n```\\r\\n// ... some code\\r\\nconsole.log(fruit);\\r\\nvar fruit = \\\"bananas\\\";\\r\\n// ... some more code\\r\\n```\\r\\n\\r\\nDuring runtime, what will be the output of this segment?\",\"explanationMarkdown\":\"The correct answer is:\\r\\n\u003e undefined\\r\\n\\r\\nThe phenomenon at play is one of Javascript's stranger quirks, called hoisting.\\r\\n\\r\\nBasically, any statement involving the `var` keyword gets \\\"hoisted\\\" to the top of its scope. So, even though we use the `fruit` variable before we delcare it, the statement that initializes `fruit` runs before we print the content of `fruit`.\\r\\n\\r\\nHowever, this does not mean that the code will print `bananas` correctly! Javascript only hoists initializations, not declarations. In other words, the code is equivalent to the following:\\r\\n\\r\\n```\\r\\nvar fruit;\\r\\nconsole.log(fruit);\\r\\nfruit = \\\"bananas\\\";\\r\\n```\\r\\n\\r\\nWhen a variable like `fruit` is initialized without a value, it is `undefined` (as opposed to `null`). Thus, the code will not throw an error, and print `undefined`.\\r\\n\\r\\nFor more on hoisting, [MDN's page](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting).\",\"answers\":[{\"isCorrect\":false,\"answerMarkdown\":\"null\"},{\"isCorrect\":false,\"answerMarkdown\":\"Uncaught ReferenceError: y is not defined\"},{\"isCorrect\":false,\"answerMarkdown\":\"bananas\"},{\"isCorrect\":true,\"answerMarkdown\":\"undefined\"}],\"topics\":[\"vanilla-js\"]}",
"sig": "ee990a4624d302ad9ddfe06ae39af57b46ebd1be6fb397fa890ef6de0660ced13057db8a2d5a90e83237f5f175e3af8f51197dbb64d44d348b7de10e47cb266a"
}