Skip to content

Instantly share code, notes, and snippets.

// Type definition for wand using tuple
type Wand = [core: string, length: number, material: string]
// Enum for house names
enum House {
Gryffindor = 'Gryffindor',
Slytherin = 'Slytherin'
}
// Type for character
type Message = number | true | 'goodbye'
const message1 = 100
const message2 = true
const message3 = 'goodbye'
const response: Message[] = [true, 'goodbye', true, 100, 42]
const firstResponse = response[0]
<script>
const localCounter = sessionStorage.getItem('counter')
console.log('savedCounter:', localCounter)
console.log('typeof savedCounter:', typeof localCounter)
const counter = Number(localCounter)
const newCounter = counter + 1
const newCounterString = String(newCounter)
sessionStorage.setItem('counter', newCounterString)
</script>
<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)