What is the correct JavaScript syntax to write "Hello World"?
-
"Hello World"
-
document.write("Hello World")
-
response.write("Hello World")
-
("Hello World")
document.write('Hello World') is valid JavaScript syntax that outputs text directly to the HTML document. This method writes during page parsing and is commonly used for simple output scenarios. Option A is just a string literal without any execution context. Option C uses ASP syntax (response.write), not JavaScript. Option D is syntactically incomplete - just parentheses without a function call.
To answer this question, you need to have a basic understanding of JavaScript syntax for displaying text on a webpage. Let's go through each option to understand why it is correct or incorrect:
Option A) "Hello World" - This option is incorrect because it is just a string literal and does not display the text on a webpage.
Option B) document.write("Hello World") - This option is correct because it uses the document.write() method to display the text "Hello World" on a webpage.
Option C) response.write("Hello World") - This option is incorrect because response.write() is not a valid JavaScript method for displaying text on a webpage.
Option D) ("Hello World") - This option is incorrect because it is missing a statement or method to display the text on a webpage.
The correct answer is B) document.write("Hello World"). This option is correct because it uses the document.write() method to display the text "Hello World" on a webpage.