Each block of JavaScript below can be run in a standard web app to see the same results described. It is recommended that you copy and paste the code and make changes to build a better understanding of how things work.
var name = "Jill";
var age = 28;
var isStudent = false;
var textOut = name + " is " + age + " years old. ";
document.querySelector("body").innerHTML = textOut;
Jill is 28 years old.
var name = "Jill";
var age = 28;
var isStudent = false;
var textOut = `${name} is ${age} years old. `;
document.querySelector("body").innerHTML = textOut;
Jill is 28 years old.
var name = "Jill";
var age = 28;
var isStudent = false;
var studentText = "";
var drivingText = "";
if (isStudent) {
studentText = "is a student";
}
else {
studentText = "is not a student";
}
if (age >= 16) {
drivingText = "can drive";
}
else {
drivingText = "cannot drive";
}
var textOut = `${name} is ${age} years old and ${studentText}. ${name} ${drivingText}.`;
document.querySelector("body").innerHTML = textOut;
Jill is 28 years old and is not a student. Jill can drive.
var person = {
name: "John",
age: 22,
isStudent: true
}
var studentText = "";
var drivingText = "";
if (person.isStudent) {
studentText = "is a student";
}
else {
studentText = "is not a student";
}
if (person.age >= 16) {
drivingText = "can drive";
}
else {
drivingText = "cannot drive";
}
var textOut = `${person.name} is ${person.age} years old and ${studentText}. ${person.name} ${drivingText}`;
document.querySelector("body").innerHTML = textOut;
John is 22 years old and is a student. John can drive
var toDos = ["Grocery shop.", "Pay bills.", "Feed dog.", "Call mom.", "Read a great book."];
var count = toDos.length;
var firstThing = toDos[0];
var secondThing = toDos[1];
var textOut = `There are ${count} items to do. ${firstThing} ${secondThing} ...`;
document.querySelector("body").innerHTML = textOut;
There are 5 items to do. Grocery shop. Pay bills. …
var toDos = ["Grocery shop.", "Pay bills.", "Feed dog.", "Call mom.", "Read a great book."];
var textOut = "";
for (var i = 0; i < toDos.length; i++) {
textOut += toDos[i] + " ";
}
document.querySelector("body").innerHTML = textOut;
Grocery shop. Pay bills. Feed dog. Call mom. Read a great book.
var toDos = ["Grocery shop.", "Pay bills.", "Feed dog.", "Call mom.", "Read a great book."];
textOut = "";
toDos.forEach(function (item, index){
textOut += item + " ";
});
document.querySelector("body").innerHTML = textOut;
Grocery shop. Pay bills. Feed dog. Call mom. Read a great book.
var toDos = [
{
content: "Grocery shop.",
complete: false
},
{
content: "Pay bills.",
complete: false
},
{
content: "Feed dog.",
complete: true
},
{
content: "Call mom.",
complete: false
},
{
content: "Read a great book.",
complete: false
}
];
textOut = "";
toDos.forEach(function (item, index){
if (!item.complete){
textOut += `<li>${item.content}</li>`;
}
});
document.querySelector("body").innerHTML = textOut;
- Grocery shop.
- Pay bills.
- Call mom.
- Read a great book.