The History of JavaScript: Everything You Need to Know (Springboard)
JavaScript History (w3schools)

Java vs. JavaScript: What's the Difference? (Coursera)
Java vs. Javascript: Which Is Right for You? (codecademy)
JavaScript is a client-side processing language:
A browser (aka the "client") reads code and then runs a program
JavaScript, HTML and CSS used together allow developers to build dynamic web pages and applications that respond respond to interaction or input from users.
Carousels, sliders, dropdown navigation, etc.
Image Credit: Bizggro.com
Eric Elliott (Medium)
(Recommended for this workshop)
Similar to writing CSS inside of HTML with the style tag,
JavaScript can be written inside of HTML with the script tag
The browser reads anything inside of the script tag as code, not
content
<script>
Javascript code goes here
</script>
<script> tags inside
your index.html file
alert('Hello World!');
"RUN" button at the top of REPL
<script>
alert('Hello World!');
</script>
Similar to CSS again, JavaScript can be separated into its own file and the file linked to the HTML file
<script src="path/file.js"></script>
script.js fileindex.html file, copy the code between the script
tags and paste the code to the script.js file in REPL
"src" attribute to the opening script tagsrc value (See code snippet below)
<script src="script.js"></script>
script.js file
console.log('Hello World!');
index.html page in a new windowCommand + Option + J
Control + Shift + J
console.log('JavaScript rules!')
2 + 3 + 10alert() opens a pop-up modal
alert('Hello World!');
console.log() prints to the console
console.log('Hello World!');
document.write()
Another way to output JavaScript is to an HTML page
There are several methods to do this; document.write() is a quick
and simple one
document.write('Finally, text on an actual page!');
Caveat: This method should only be used for testing; never in actual or production code
HTML DOM Document write() (w3schools)
You can leave comments in your code—these are notes that people can read but that browsers will ignore
/* */ to write multi-line
comments
// to write single-line comments
/*
I can write long comments or comments that
span multiple lines: Lorem ipsum dolor, sit
amet consectetur adipisicing elit. In, iure?
*/
// OR Write one line comments
console.log('Hello!'); // My first JavaScript code
In your
script.js
file:
alert messageconsole.log message and check it in the consoledocument.write
Photo credit: Giulia Gasparro
To declare (create) a variable, type the word
let and the variable name.
let numberOfKittens;
It is a good idea to give your variable a starting value. This is called initializing the variable.
let numberOfKittens = 5;
undefined
$ (the dollar sign), or _ (an underscore)
Once you have created a variable, you can use it in your code
let numberOfKittens = 5;
console.log(numberOfKittens);
In your script.js file:
console.log() or alert() or
document.write()
string: string of characterslet userName = 'Jane Lane';
number: integer or floating points (decimal)
let myAge = 30;
let price = 34.99
Photo credit: Mike Lawson
let kittensName = 'Fluffy';
console.log('I\'d like to use an apostrophe');
aka combining strings
Photo credit: Matt
We can join or link strings together with a
+ (plus) sign, which is also known as
the string operator or concatenation operator
let kittensName = 'Fluffy ';
let fullName = kittensName + 'McDougle';
console.log(fullName); // Outputs 'Fluffy McDougle'
In your
script.js
file:
Photo credit: WJ van den Eijkhof
Variables can be numbers--either integers (whole numbers) or floats (decimals)
let numberOfKittens = 5;
let cutenessRating = 9.6;
When variables store numbers as their values, we can perform calculations with variable names!
let numberOfKittens = 5;
let numberOfPuppies = 4;
let numberOfAnimals = numberOfKittens + numberOfPuppies; // 9
| Example | Name | Result |
|---|---|---|
-a |
Negation | Opposite of a. |
a + b |
Addition | Sum of a and b. |
a - b |
Subtraction | Difference of a and b. |
a * b |
Multiplication | Product of a and b. |
a / b |
Division | Quotient of a and b. |
a % b |
Modulus | Remainder of a divided by b. |
In your
script.js
file:
let numberOfFruit = 6;
let typeOfFruit = 'bananas';
let allTheFruit = 'I have ' + numberOfFruit + ' ' + typeOfFruit + '!';
console.log(allTheFruit);
// Result: I have 6 bananas!
Time to build your first JavaScript "app"!
Your code should:
Photo credit: DennisM2
Control flow in JavaScript is how the computer runs code from top to bottom, until it hits a statement, structure or code that changes the flow
Control Flow (MDN)
if Statement
Use if statements to decide which lines
of code to execute, based on a condition
if (condition) {
// statements to execute
}
let age = 30;
if (age > 18) { // if age is greater than 30
console.log('You are an adult');
}
| Operator | Name |
|---|---|
a === b |
Identical |
a !== b |
Not identical |
a < b |
Less than |
a > b |
Greater than |
a <= b |
Less than or equal to |
a >= b |
Greater than or equal to |
temperature
let age = 30;
if (age > 18) { // if age is greater than 30
console.log('You are an adult');
}
Syntax example
Photo credit: Thomas Hawk
Use the else statement to provide an alternate set of
instructions
if (condition) {
// what to do if condition is true
} else {
// what to do if condition is not true
}
if (condition) {
// code to execute if true
} else {
// code to execute if false
}
let age = 30;
if (age >= 16) { // Greater than or equal to
console.log('Yay, you can drive!');
} else {
console.log('Sorry, you have to wait ' +
(16-age) + ' until you can drive.');
}
Modify your "wear a coat" code for these new conditions:
Photo credit: Rick Payette
What is a Function? JavaScript Function Examples (FreeCodeCamp)
function keyword
(), and then followed by curly
brackets {}
function functionName() {
CODE INSTRUCTION
}
Similar to variables, function names:
function displayParrotFacts() {
console.log('Some parrot species can live for over 80 years');
}
But declaring a function is not enough; the function needs to be called or invoked
To invoke (run) a function, type its name, followed by parentheses
()
functionName();
displayParrotFacts();
This is the year I learn JavaScript!
function functionName(){
Code Instruction
}
functionName();
We can declare and work with variables inside a function
function displayFullName(){
let firstName = 'Kamala';
let lastName = 'Harris'
console.log(firstName + ' ' + lastName);
}
displayFullName();
Functions can be declared with
parameters and can run or accept input
called
arguments
// kittenName is a parameter
function callKitten(kittenName){
console.log('Come here, ' + kittenName + '!');
}
// 'Fluffy' is an argument
callKitten('Fluffy'); // Output: 'Come here, Fluffy!'
parameters are placeholders for the
actual values (arguments) that we
specify for the function to use when it is invoked or called
function addNumbers(a, b){ // a and b are parameters
console.log(a + b);
}
// 5 and 7 are arguments
addNumbers(5, 7); // Outputs: 12
// Another Example:
let num1 = 9
let num2 = 12
// num1 and num2 are arguments
addNumbers(num1, num2); // Outputs: 21
Parameters and Arguments make functions reusable
Writing functions with parameters and arguments
Strings. Numbers. If/Else Statements. Functions.
Photo credit: Hannah Busing
Take the survey linked in the course's video description.