An Hour of JavaScript
Terms defined: arrow notation, asynchronous operation, dotted notation, promise, string interpolation, truthiness
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
- Install deno for command-line JavaScript
Variables, Types, and Control Flow
console.log('hello, world');
- Run program as
deno run program.js consoleis a built-in module- Use the same dotted notation as Python
- Trailing semi-colon is no longer required but commonly used
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`);
}
}
constdefines a constant- The array contents aren't constant: the reference is
for…initerates over the indices of an array- Not recommended for arrays, and can also include inherited properties
- Use
for…ofto iterate directly over the values of an array. - To loop over both the indices and values of an array, use
forEachorentries()withfor…of - Parentheses are required
typeofis an operator not a function, and returns a stringif/else if/elsework as expected- Use back-quotes and
${…}for string interpolation - JavaScript considers an empty array to be truthy
- Always use
===(triple equals) and!==for equality - Otherwise JavaScript does some strange type conversions
Objects
const person = {
'staff_id': 31,
'personal': 'Loan',
'family': 'Nguyen'
};
console.log(`personal: ${person.personal}`);
console.log(`family: ${person['family']}`);
- JavaScript objects are (kind of) like Python dictionaries
- Create so many of these that keys don't have to be quoted
const person = {
staff_id: 31,
personal: 'Loan',
family: 'Nguyen'
};
console.log(`personal: ${person.personal}`);
- Can use variables' names and values to construct objects
const staff_id = 31;
const personal = 'Loan';
const family = 'Nguyen';
const person = {staff_id, personal, family};
console.log(`person: ${JSON.stringify(person)}`);
JSONis another built-in module- Its
stringifyfunction does what its name suggests - Without this,
personis displayed as[Object object]
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);
letintroduces a variable- The older
varis still sometimes used - Functions take parameters, create a scope, etc.
const largest = (left, right) => {
if (left < right) {
return left;
} else {
return right;
}
}
console.log(`largest(3, 5) is ${largest(3, 5)}`);
- A function is just another kind of object
- Can define using arrow notation and then assign to a variable
- Handy in a language that uses lots of little functions
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}`);
});
- A promise represents a delayed operation
- Use
.thenmethod to tell JavaScript what to do when a value becomes available- Use this a lot in web programming so that the browser won't freeze up
const value = await Deno.readTextFile('left.txt');
console.log(`left value is ${value}`);
awaitmakes asynchronous operations more readable