JAVASCRIPT W3SCHOOLS
QUESTIONS AND ANSWERS
document.getElementById("demo").innerHTML = "Hello JavaScript"; - ANSWER-Get
element by ID demo and replace the text in it with 'Hello JavaSript'
onclick="document.getElementById('myImage').src='pic_bulboff.gif'" - ANSWER-
Change the src on click
document.getElementById("demo").style.display = "none"; - ANSWER-Hide the demo
element
document.getElementById("demo").style.display = "block"; - ANSWER-Display the
'demo' element.
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script> - ANSWER-In HTML, JavaScript code must be inserted between <script> and
</script> tags.
JavaScript in <head> or <body>? - ANSWER-Scripts can be placed in the <body>, or in
the <head> section of an HTML page, or in both.
<script src="myScript.js"></script> - ANSWER-Scripts can also be placed in external
files:
<script src="https://www.w3schools.com/js/myScript1.js"></script> - ANSWER-External
scripts can be referenced with a full URL or with a path relative to the current web page.
<script src="/js/myScript1.js"></script> - ANSWER-This example uses a script located in
a specified folder on the current web site:
<script src="myScript1.js"></script> - ANSWER-This example links to a script located in
the same folder as the current page
document.write(5 + 6); - ANSWER-Using document.write() after an HTML document is
fully loaded, will delete all existing HTML:
window.alert(5 + 6); - ANSWER-You can use an alert box to display data:
, console.log(5 + 6); - ANSWER-For debugging purposes, you can use the console.log()
method to display data.
var person = "John Doe", carName = "Volvo", price = 200; - ANSWER-You can declare
many variables in one statement.
Start the statement with var and separate the variables by comma:
var person = "John Doe",
carName = "Volvo",
price = 200; - ANSWER-A declaration can span multiple lines:
z = "Hello" + 5; - ANSWER-Adding a number and a string will return a string.
&& - ANSWER-logical and
|| - ANSWER-logical or
! - ANSWER-logical not
typeof - ANSWER-Returns the type of a variable
instanceof - ANSWER-Returns true if an object is an instance of an object type
person.name = person["name"] - ANSWER-Member
++ -- - ANSWER-Prefix Increment, Prefix Decrement
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; - ANSWER-
JavaScript objects are written with curly braces.
Object properties are written as name:value pairs, separated by commas.
var cars = ["Saab", "Volvo", "BMW"]; - ANSWER-JavaScript arrays are written with
square brackets.
Array items are separated by commas.
Array indexes are zero-based, which means the first item is [0], second is [1], and so
on.
typeof "John"
QUESTIONS AND ANSWERS
document.getElementById("demo").innerHTML = "Hello JavaScript"; - ANSWER-Get
element by ID demo and replace the text in it with 'Hello JavaSript'
onclick="document.getElementById('myImage').src='pic_bulboff.gif'" - ANSWER-
Change the src on click
document.getElementById("demo").style.display = "none"; - ANSWER-Hide the demo
element
document.getElementById("demo").style.display = "block"; - ANSWER-Display the
'demo' element.
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script> - ANSWER-In HTML, JavaScript code must be inserted between <script> and
</script> tags.
JavaScript in <head> or <body>? - ANSWER-Scripts can be placed in the <body>, or in
the <head> section of an HTML page, or in both.
<script src="myScript.js"></script> - ANSWER-Scripts can also be placed in external
files:
<script src="https://www.w3schools.com/js/myScript1.js"></script> - ANSWER-External
scripts can be referenced with a full URL or with a path relative to the current web page.
<script src="/js/myScript1.js"></script> - ANSWER-This example uses a script located in
a specified folder on the current web site:
<script src="myScript1.js"></script> - ANSWER-This example links to a script located in
the same folder as the current page
document.write(5 + 6); - ANSWER-Using document.write() after an HTML document is
fully loaded, will delete all existing HTML:
window.alert(5 + 6); - ANSWER-You can use an alert box to display data:
, console.log(5 + 6); - ANSWER-For debugging purposes, you can use the console.log()
method to display data.
var person = "John Doe", carName = "Volvo", price = 200; - ANSWER-You can declare
many variables in one statement.
Start the statement with var and separate the variables by comma:
var person = "John Doe",
carName = "Volvo",
price = 200; - ANSWER-A declaration can span multiple lines:
z = "Hello" + 5; - ANSWER-Adding a number and a string will return a string.
&& - ANSWER-logical and
|| - ANSWER-logical or
! - ANSWER-logical not
typeof - ANSWER-Returns the type of a variable
instanceof - ANSWER-Returns true if an object is an instance of an object type
person.name = person["name"] - ANSWER-Member
++ -- - ANSWER-Prefix Increment, Prefix Decrement
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; - ANSWER-
JavaScript objects are written with curly braces.
Object properties are written as name:value pairs, separated by commas.
var cars = ["Saab", "Volvo", "BMW"]; - ANSWER-JavaScript arrays are written with
square brackets.
Array items are separated by commas.
Array indexes are zero-based, which means the first item is [0], second is [1], and so
on.
typeof "John"