16
loading...
This website collects cookies to deliver better user experience
for (variable in object) {
statement
}
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
// expected output:
// "a: 1"
// "b: 2"
// "c: 3"
for (variable in object) {
statement
}
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
// expected output: "a"
// expected output: "b"
// expected output: "c"
<div id='example'>
<strong>This sentence is bold!</strong>
</div>
element.innerText
and element.innerHTML
, we can see what we get from the example code.const sentence = document.getElementById('example')
sentence.innerText
// => "This sentence is bold!"
const sentence = document.getElementById('example')
sentence.innerHTML
// => <strong>This sentence is bold!</strong>
==
all the time to compare things. I have to say I was weirded out by ===
in Javascript. It's just so unnecessarily long-looking and I have to do an additional key stroke. JavaScript has both ==
and ===
, but it's better to use ===
. The double equals operator is an abstract comparison and the triple equals operator is a strict comparison.const a = 2
const b = 2
a == b
// => true
a === b
// => true
const a = 2
const b = 2
const c = "2"
a == b
// => true
a === b
// => true
a == c
// => true
a === c
// => false
a == c
returns true because both variables contain the same value even though they have different types. The double equals operator is converting the two variables to the same data type and then comparing the value. a === c
returns false because the types of variables are different even though the value are the same. Overall, the triple equals operator is often used more than the double equals operator due to it's strict comparison of datatype and value making it a better and more accurate comparison.