Skip to content

Instantly share code, notes, and snippets.

<script>
const localCounter = localStorage.getItem('counter')
console.log('savedCounter:', localCounter)
console.log('typeof savedCounter:', typeof localCounter)
const counter = Number(localCounter)
const newCounter = counter + 1
const newCounterString = String(newCounter)
localStorage.setItem('counter', newCounterString)
</script>
<script>
console.log('before')
async function main () {
const uploadData = { message: 'ready' }
const uploadJson = JSON.stringify(uploadData)
const init = { body: uploadJson, method: 'POST' }
// { body: JSON.stringify(uploadData), method: 'POST' }
const dogResponse = await fetch('https://dog.ceo/api/breeds/image/random', init)
}
main() // asynchronous / non-blocking
<img id="dog" width="500"/>
<script>
console.log('before')
async function main () {
console.log('starting dog download...')
const dogResponse = await fetch('https://dog.ceo/api/breeds/image/random')
const dogData = await dogResponse.json()
console.log('dogData', dogData)
<script>
// First log 'start'
// Then log "starting alpha"
// Then, sleep for 2 seconds
// Then, log 'alpha'
// Then, log "starting beta"
// Then, 4 seconds later, log beta
// Then, log "starting gamma"
// Then, 1 second later log gamma
// Then, log "starting delta"
<script>
// First log 'start'
// Then log "starting alpha"
// Then, sleep for 2 seconds
// Then, log 'alpha'
// Then, log "starting beta"
// Then, 4 seconds later, log beta
// Then, log "starting gamma"
// Then, 1 second later log gamma
// Then, log "starting delta"
<script>
// First log 'start'
// Then log "starting alpha"
// Then, 2 seconds later, log alpha
// Then, log "starting "beta"
// Then, 4 seconds later, log beta
// Then, log "starting gamma"
// Then, 1 second later log gamma
// Then, log "starting delta"
// Then, 3 seconds later log delta
const x = 'hello!!!'
console.log(x)
const y = 42
type List = [first: string, second: string, third: number]
const list: List = ['a', 'b', 3]
function describe <T> (value?: T) {
console.log('The value is:', value)
// 9. Write a program to reverse an array
var letters = ['a', 'b', 'c']
letters.reverse()
console.log(letters)
// 8. Write a program to create an array of objects "employees" with properties "name", "age", and "city" and set their respective values to
// "John", 30, "New York", "Thomas", 40, "Chicago", "Lily", 35, "San Francisco". Print the name, age, and city of each employee.
// Print the name, age, and city of each employee using for..of loop.
var employees = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Thomas', age: 40, city: 'Chicago' },
{ name: 'Lily', age: 35, city: 'San Francisco' }
]
for (var employee of employees) {
// 7. Declare an object named "toy" with an empty object as its initial value.
// Add the properties "name" and "category" with values "Super Space Rocket" and "Action Figures & Playsets" respectively.
var toy = {}
toy.name = 'Super Space Rocket'
toy.category = 'Action Figures & Playsets'
console.log(toy)
function describeToy (toy, name, category) {
toy.name = name