This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With no type checks, the code seems to work fine. | |
const user1 = { id: 1, name: "Erowyn" } | |
const user2 = { id: 2, name: "Lavendula" } | |
const user3 = { id: 3, name: "Fralia" } | |
const userArray = [user1, user2, user3] | |
for (const user of userArray) { | |
console.log(`${user.id}: ${user.name}`) | |
} | |
// Output: | |
// 1: Erowyn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const object = { "🔑key🔑": "💸value💸" } | |
console.log(typeof object) // "object" | |
console.log(object !== null) // true | |
console.log(object != null) // true | |
console.log(typeof null) // "object" | |
console.log(null !== null) // false | |
console.log(null != null) // false | |
console.log(typeof undefined) // "undefined" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// && can be used to prevent errors, because the error won't be thrown. | |
const nullBanana = null | |
try { | |
console.log(nullBanana.length) | |
} catch (e) { | |
console.log(e) | |
} | |
// Output: "TypeError: null has no properties." | |
// Using && short-circuits the code, so the TypeError doesn't occur. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using && will prevent nonsense code from being executed. | |
false && console.log(NoNsEnSe_CoDE) // nothing happens | |
// You'll frequently see && used for type-checking. | |
const bananas = "🍌🍌🍌🍌🍌" | |
typeof bananas === "string" && console.log(bananas) // 🍌🍌🍌🍌🍌 | |
// For example, you might be expecting an array, not a string. | |
Array.isArray(bananas) && bananas.push("🍌") // nothing happens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The typeof null is "object" in JavaScript. | |
console.log(typeof null === "object") // true | |
// Let's look at type checking a Date object. | |
const date = new Date() | |
console.log(typeof date) // "object" | |
console.log(Object.prototype.toString.call(date)) // [object Date] | |
console.log(date.constructor) // function Date() | |
console.log(date.constructor.name) // "Date" | |
console.log(date instanceof Date) // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addWeirdStuffMap = (arrayOne, arrayTwo) => { | |
const [sumOfEvensInArrayTwo, sumOfOddsInArrayTwo] = arrayTwo.reduce( | |
([sumOfEvens, sumOfOdds], itemTwo) => { | |
return itemTwo % 2 === 0 | |
? [sumOfEvens + itemTwo, sumOfOdds] | |
: [sumOfEvens, sumOfOdds + itemTwo] | |
}, | |
[0, 0] // Start with an initial value of 0 for both sums | |
) | |
// Add one to each itemOne if arrayTwo contains an element greater than 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addWeirdStuffRefactor = (arrayOne, arrayTwo) => { | |
const sumOfEvensInArrayTwo = arrayTwo.reduce( | |
(sumOfEvens, itemTwo) => | |
itemTwo % 2 === 0 ? sumOfEvens + itemTwo : sumOfEvens, | |
0 // Start with an initial value of 0 | |
) | |
const sumOfOddsInArrayTwo = arrayTwo.reduce( | |
(sumOfOdds, itemTwo) => | |
itemTwo % 2 === 1 ? sumOfOdds + itemTwo : sumOfOdds, | |
0 // Start with an initial value of 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addWeirdStuff = (arrayOne, arrayTwo) => { | |
let sumOfEvensInArrayTwo = 0 | |
let sumOfOddsInArrayTwo = 0 | |
let addOneToArrayOne = 0 // if an element of arrayTwo is over 20 | |
for (const itemTwo of arrayTwo) { | |
if (itemTwo % 2 === 0) { | |
sumOfEvensInArrayTwo += itemTwo | |
} | |
if (itemTwo % 2 === 1) { | |
sumOfOddsInArrayTwo += itemTwo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enter these commands into the console to load jQuery first. | |
// <script src="https://code.jquery.com/jquery-3.5.0.js"></script> | |
const scriptSource = "https://code.jquery.com/jquery-3.5.0.js" | |
document.write(`<script src="${scriptSource}" type="text/javascript"></script>`) | |
// Then, enter the following commands separately: | |
console.log($) // function jQuery(selector, context) | |
console.log(typeof $) // "function" | |
console.log($()) // Object { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log($) // function() | |
console.log(typeof $) // "function" | |
console.log($()) // null | |
console.log(typeof $()) // "object" | |
try { | |
console.log($().jquery) | |
console.log(typeof $().jquery) | |
} catch (e) { |
NewerOlder