Today, we stop being users of technology and start becoming its creators.
Imagine you have a house help who is:
Programming is the art of writing perfect, unambiguous instructions for our dumb but fast house help: the computer.
Consider the sentence:
"I saw the man on the hill with a telescope."
A computer cannot guess. It needs a language where every word and symbol has one, and only one, meaning.
We need a special program called an Interpreter to translate our high-level JavaScript into low-level machine code the computer can execute.
In programming, the first thing you learn is how to make the computer display a message.
console.log('Hello, World!');
The fastest way to get feedback.
Ctrl+Shift+I
or
Cmd+Option+I
).
> console.log('Hello from the browser console!');
This is how we'll run most of our code.
touch app.js
console.log('Hello from a Node.js file!');
$ node app.js
Hello from a Node.js file!
The most basic, indivisible pieces of data. The fundamental building blocks, like atoms.
Number
(for math)String
(for text)Boolean
(for truth)undefined
(accidental emptiness)null
(intentional emptiness)Number
10
-55
3.14159
// Try this in the console:
100 + 50 * 2 // result is 200
String
'Hello' // Single quotes
"World" // Double quotes
`This is special!` // Backticks (Template Literals)
// Concatenation (joining strings)
'Hello' + ' ' + 'World' // results in 'Hello World'
Boolean
true
and
false
.
true
is very different from
'true'
.
Booleans are the foundation of decision-making in code.
undefined
vs. null
Both represent emptiness, but in different ways.
undefined
null
Variables are named containers for storing data values. They are the labeled jars in our programming kitchen, bringing order to the chaos of raw data.
Working with variables is a two-step process:
let myAge; // Jar "myAge" is created. It's empty (undefined).
=
operator.
myAge = 30; // The number 30 is now in the "myAge" jar.
You can also do both at once (initialization):
let myName = 'The Architect';
let
, const
, and
var
let
: The standard. Use for variables whose value
can change.
let score = 0;
score = 100; // Allowed
const
: The safeguard. Use for variables whose value
will not change.
const birthYear = 1990;
// birthYear = 1991; // ERROR!
var
: The old way. Has confusing behavior.
We will not use it.
Best Practice: Use const
by default.
Only use let
when you know you'll need to re-assign
the value.
These operators compare two values and always result in a
boolean (true
or
false
).
===
: Strict equality (Are the value AND type the
same?)
!==
: Strict inequality>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toGolden Rule: Always use strict equality (===). Avoid loose equality (==) which can have confusing behavior (e.g., 77 == '77' is true!).
&&
(AND): Is the condition on the
left AND the condition on the right true? (Both must be true)
||
(OR): Is the condition on the
left OR the condition on the right true? (At least one must be
true)
!
(NOT): Flips a boolean value.
!true becomes false.
if / else
This is the heart of programming. It allows our "butler" to take different paths based on the answers to our questions.
const age = 20;
if (age >= 18) {
console.log('You are eligible to vote.');
} else {
console.log('You are not yet eligible to vote.');
}
const score = 85;
if (score >= 90) {
console.log('You get an A!');
} else if (score >= 80) {
console.log('You get a B!');
} else if (score >= 70) {
console.log('You get a C.');
} else {
console.log('You need to study more.');
}
Let's apply everything we've learned to build an interactive story.