Skip to content

Instantly share code, notes, and snippets.

View djD-REK's full-sized avatar
🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞

Dr. Derek Austin djD-REK

🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞
View GitHub Profile
@djD-REK
djD-REK / Can JavaScript Arrays Contain Different Types.js
Created December 17, 2020 23:13
Can JavaScript Arrays Contain Different Types
// 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
@djD-REK
djD-REK / How to Check for an Object (Javascript Object Null Check).js
Created December 17, 2020 19:34
How to Check for an Object (Javascript Object Null Check)
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"
@djD-REK
djD-REK / What Is The Short-Circuit Operator in JavaScript && (Logical AND) 2.js
Created December 15, 2020 23:52
What Is The Short-Circuit Operator in JavaScript && (Logical AND) 2
// && 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.
@djD-REK
djD-REK / What Is The Short-Circuit Operator in JavaScript && (Logical AND) 1.js
Created December 15, 2020 23:52
What Is The Short-Circuit Operator in JavaScript && (Logical AND) 1
// 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
@djD-REK
djD-REK / A Better Method of Type Checking JavaScript Objects - Object.getPrototypeOf().js
Created December 15, 2020 01:06
A Better Method of Type Checking JavaScript Objects - Object.getPrototypeOf()
// 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
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
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
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
// 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 { }
console.log($) // function()
console.log(typeof $) // "function"
console.log($()) // null
console.log(typeof $()) // "object"
try {
console.log($().jquery)
console.log(typeof $().jquery)
} catch (e) {