From Zero To Dangerous
Further Reading:
Look for the links
One of the Big 3 in Web Development:
🤔 But wait, can't CSS make things interactive?
CSS requires everything to be there on page load. JS has more freedom and more power:
Browser/HTML APIs:
Third Party APIs:
But it's on every browser which is why everyone uses it!
My Opinion:
Learn new JavaScript because they're making it better for a reason. ES5 & ES6 are compatible, so write what you know and sprinkle in new stuff as you learn it or need it.
My Warning:
If you know you need to support old browsers, then learn ES6 and how to transpile it into an older version. (Next talk, maybe?)
Using a Library or Framework doesn't mean you can avoid using JavaScript.
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<h1>My HTML Goes Here</h1>
<script src="main.js"></script>
</body>
</html>
Because now you have to deal with time.
JS happens line by line for the most part. But it can skip lines, jump back, re-run, or stop altogether. It is procedural.
This can be the most frustrating thing about learning a programming language.
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript</title>
<link href="styles.css" rel="stylesheet">
<script src="main.js"></script>
</head>
<body>
<h1>My HTML Goes Here</h1>
<button>Make Something Happen</button>
</body>
</html>
When the browser reads the JavaScript on line 8 which is all about the button, the browser hasn't made the button yet. That's line 13.
A button that should do something:
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<h1>My HTML Goes Here</h1>
<button>Make Something Happen</button>
<script src="main.js"></script>
</body>
</html>
Now the browser makes the button on line 11 which is just in time for the JavaScript to go looking for it on line 13.
A button that actually does something:
This can be the other most frustrating thing about learning a programming language.
10px is a length
red is a color
10 is a Number
"red" is a String
// Numbers
2 + 2
13 - 5
24 * 7
1 / 4
// Strings
'Hello World'
"con" + "cat" + "e" + "nate"
`half of 100 is ${100 / 2}`
// Booleans
true
3 > 2
true == 3 > 2
// This is a single line comment
// JS doesn't read it, but you can!
2 + 2 // They can even come after real lines
/*
Here is a
multi-line
comment
*/
Use comments to describe WHY you're doing something so you remember later.
const myName = "Zach Fedor"
let timesEaten = 2
const imHungry = timesEaten < 3
timesEaten += 1
Two things are happening on line 1:
The variable myName is declared
The value "Zach Fedor" is assigned to it
Variables declared with let can be assigned new values while those declared with const cannot.
You can't re-declare the same variable name.
let 1 = 10
let let = 100
This will break, too
'I'm supposed to be a string'
All of this is broken code.
const myObject = {
key: 'value',
name: 'Zach Fedor',
}
myObject.name // 'Zach Fedor'
myObject.newKey = 13
2 + myObject.newKey
Objects are the grandaddy of data types
const myArray = [
'apples',
'bananas',
'cherries',
]
myArray[0] // 'apples'
myArray.push('dates')
// ['apples', 'bananas', 'cherries', 'dates']
Arrays are special objects where the keys are numbers
alert("Hello!")
myArray.push('new item')
myArray.push('another new item')
"some string".indexOf('t') // 6
console.log("Print to the browser console")
const average = (x, y) => {
return (x + y) / 2
}
average(5, 10) // 7.5
average(1, 10) // 5.5
average(1, 'hello') // NaN
// This function takes no arguments and does nothing
const myFunction = () => {}
// Function Expression
const average = function(x, y) {
return (x + y) / 2
}
// Function Declaration
function average(x, y) {
return (x + y) / 2
}
if (condition) {
doSomething()
} else if (condition2) {
doOtherThing()
} else {
doLastThing()
}
You also have a few ways to run the same code multiple times
for (let i = 1; i <= 5; i++) {
console.log(i)
}
const myArray = [1, 2, 3, 4, 5]
for (let i of myArray) {
console.log(i)
})
const bodyElement = document.querySelector('body')
const newParagraph = document.createElement('p')
newParagraph.textContent = "I didn't exist before"
bodyElement.appendChild(newParagraph)
Using the browser's DOM API, you can control the rendered HTML and CSS. This example:
Disclaimer: This is only true for your own code!
If you work on a team, just follow their lead. Once you have opinions, feel free to voice them, but remember there might be reasons you don't know about.
But most importantly, go build something!