An Hour of JavaScript

JavaScript is the only language that I'm aware of that people feel they don't need to learn before they start using it.

— Douglas Crockford

Variables, Types, and Control Flow

console.log('hello, world');
const values = [0, 1, '', 'text', undefined, null, [], [2, 3]];
for (const val of values) {
    const type = typeof val;
    if (val) {
        console.log(`val '${val}' of type ${type} seems true`);
    } else {
        console.log(`val '${val}' of type ${type} seems false`);
    }
}

Objects

const person = {
    'staff_id': 31,
    'personal': 'Loan',
    'family': 'Nguyen'
};
console.log(`personal: ${person.personal}`);
console.log(`family: ${person['family']}`);
const person = {
    staff_id: 31,
    personal: 'Loan',
    family: 'Nguyen'
};
console.log(`personal: ${person.personal}`);
const staff_id = 31;
const personal = 'Loan';
const family = 'Nguyen';
const person = {staff_id, personal, family};
console.log(`person: ${JSON.stringify(person)}`);

Functions

function limits (values) {
  if (!values.length) {
    return [undefined, undefined];
  }
  let low = values[0];
  let high = values[0];
  for (let v of values) {
    if (v < low) {
        low = v;
    }
    if (v > high) {
        high = v;
    }
  }
  return [low, high]
}

const result = limits([1, -3, 2, 7]);
console.log(result);
const largest = (left, right) => {
    if (left < right) {
        return left;
    } else {
        return right;
    }
}

console.log(`largest(3, 5) is ${largest(3, 5)}`);
const values = [1, -3, 5, -7];

const is_positive = values.map(v => v >= 0);
console.log(`is_positive: ${is_positive}`);

const keep_positive = values.filter(v => v >= 0);
console.log(`keep_positive: ${keep_positive}`);

const print_positive = values.forEach(v => {
    if (v >= 0) {
        console.log(v);
    }
});

Asynchronous Operations

const middle = Deno.readTextFile('middle.txt');
console.log(`middle is ${middle}`);
middle.then(value => {
    console.log(`middle.then is ${value}`);
});
const value = await Deno.readTextFile('left.txt');
console.log(`left value is ${value}`);