- Introduction, Basic Data Types, Operators
- Variables, Console, Comments
- If statement, Loops, Arrays
- Functions
- Objects & More
- DOM
- Callbacks
- Web APIs
- OOP
- Libraries, LocalStorage
Direct link to lessons: 1, 2, 3, 4, 5, 6, 8, 9, 10, 11
Lesson 1: Introduction, Basic Data Types, Operators
It does not matter what we cover, but what you discover (Noam Chomsky)
HTML | CSS | JavaScript |
---|---|---|
Content | Presentation | Dynamic Effects |
Nouns | Adjectives | Verbs |
<p></p> <!-- HTML: Adds a paragraph -->
P {color: red;} /* CSS: Makes the paragraph red */
p.remove(); // JavaScript: removes the paragraph
- JS is a lightweight, cross-platform, object-oriented programming language
- JS is one of three core technologies of web development
- JS makes modern web-development possible
- Dynamic effects
- Modern web applications that we can interact with
- JS can be used for much more, though...
- For Server-side: Node.js
- For mobile apps: Cordova or React Native
- For desktop apps: Progressive Web Apps, Elektron
- "embedded" JS engines: QtQuick, Rhino/Nashorn
We'll be using these tools during our course:
- https://www.google.com/chrome/
- Most common browser
- Most up to date
- Excellent Development Tools (F12)
- https://code.visualstudio.com/
- Open source editor
- Great tools
- Good defaults
- Fast
- https://developer.mozilla.org/en-US/
- Up to date reference for JS
- Good guides
- Free
- freecodecamp.org
- udemy.com: Introduction to JavaScript Development
- codeacademy.com: Introduction to JavaScript
- edabit.com: Learn JavaScript with interactive challenges and external resources
- Standardized Specification of JavaScript
- Very technical, not good for learning
Character | Character |
---|---|
( Parentheses ) |
& Ampersand & |
{ Braces, or curly braces } |
* Asterisk * |
[ Brackets, or square brackets ] |
^ Caret or circumflex ^ |
< Angle brackets > |
| Vertical bar, or pipe | |
' Single quote ' |
~ Tilde ~ |
" Double quote " |
# Hash or number sign # |
` Back tick ` |
_ Underscore _ |
/ Slash, or Forward slash / |
: Colon : |
\ Backslash \ |
; Semicolon ; |
Programming involves knowing and doing the following things:
- input: Get data from the keyboard, a file, a sensor, or some other device.
- output: Display data on the screen, or send data to a file or other device.
- math: Perform basic mathematical operations like addition and division.
- decisions: Check for certain conditions and execute the appropriate code.
- repetition: Perform some action repeatedly, usually with some variation.
(Source: http://greenteapress.com/thinkpython/thinkpython.pdf, Page 3)
- Open Google Chrome
- Hit
F12
key - Click on Console
- Try entering your name
- Try entering your shoe size
- JavaScript seems to be picky about the data we enter
- In order to understand how to enter data, we need to know about Data Types
- One basic data type is called Number
- A Number can be a positive integer (
1
,2
,42
...) - or negative with a leading
-
minus sign (-1
,-2
,-42
) - A Number can also be a positive or negative floating point (
0.25
,-4.5
) - A Number can not contain fractions, only floating point (
1/3
vs.0.33333
)
- Another basic data type is called String
- A String can contain any textual data
- It starts and ends with a
"
double quote - It can also start and end with a
'
single quote - Example:
"Hello"
,'Carlo'
- The basic data type Boolean can only be
true
orfalse
- It can be used to represent logical states that can either be
true
orfalse
- You can use them for yes/no questions: "yes" (true) or "no" (false)
- Example:
true
,false
- The basic data type Undefined has only one value:
undefined
- Example:
undefined
Now, try to enter some data to the JavaScript console, but think of the data type first:
- Try entering your name
- Try entering your shoe size
- Try entering whether you are older than the person next to you
"Harald"
41
true
- Try to summarize what we learned so far about data types
Strings start and end with single ('
) or double quotes ("
). But what if we want to add a quote within our string?
"He said: "Hello""
Solution: We can escape the very next character with a backslash (\
):
"He said: \"Hello\""
If you want a backslash in a string, you need to escape it: "\\"
There are some special characters in strings, for example:
"\n"
- new line"\t"
- tab
These are called "Control Characters"
"Hello"
'World'
"He said "hello" to me"
'Let's go!'
""
'This is a\nnew line'
'This is a backslash: \'
"Hello" // CORRECT
'World' // CORRECT
"He said "hello" to me"; // WRONG - unescaped quotes
'Let's go!' // WRONG - unescaped '
"" // CORRECT
'This is a\nnew line'; // CORRECT
'This is a backslash: \'; // WRONG - escaped '
Group | Operators | Example |
---|---|---|
Numerical Operators | + - * / |
5 + 4 * 3 7 / 2 - 2 "Hello" + " World" |
Comparison Operators | === !== < > <= >= |
temperature !== 25 age >= 18 |
Logical Operators | || && ! |
a && !b x >= 5 && x < 15 |
Combined Operators | += -= *= /= ++ |
a *= 2 count++ |
Basic mathematical operators:
+
addition-
subtraction*
multiplication/
division**
exponentiation
The addition operator can be used on Strings and Numbers:
1 + 1 // is 2
"hello" + " world" // is "hello world"
1 + 1 // is 2
"1" + "1" // is "11"
Make sure to always choose the correct data type for your data, because the operators in JavaScript behave differently depending on the data type.
Compute the following with JavaScript:
- How old you will be in one year?
- What is the difference between your age and the person next to you?
- What is the square of 8?
42 + 1 // I'll be 43 next year!
42 - 20 // I'm 22 years older
8 ** 2 // Square of eight is 64
===
strict equality!==
strict inequality>
greater than<
less than>=
greater or equal<=
less or equal
Use JavaScript to compute the following:
- Figure out whether you are older than the person next to you
- Figure out whether you have the same first name as the person next to you
42 > 20 // true - I am older
"Harald" === "Abdullah" // false - not the same first name
These operators only make sense on Boolean:
&&
Logical AND||
Logical OR!
Logical NOT
AND takes two booleans and returns true
if both booleans are true
true && true // true
true && false // false
false && true // false
false && false // false
Example: I need bread (true/false) AND cheese (true/false) to make a cheese toast
OR takes two booleans and returns true
if any (or both) are true
true || true // true
true || false // true
false || true // true
false || false // false
Example: To apply to a job, I need to know German OR English.
Use JavaScript to compute:
- Am I the youngest person in a group of three?
- Am I younger than at least one person in a group of three?
Am I the youngest person in a group of three? I need to be younger than person to my left AND I need to be younger than person to my right
42 < 20 && 42 < 35 // false - I am not the youngest person
Am I younger than at least one person in a group of three? I need to be younger than person to my left OR I need to be younger than person to my right
42 < 20 || 42 < 35 // false - I am not younger than at least one other person
Logical NOT negates a boolean:
!true // false
!false // true
Example: Am I NOT the youngest person?
- Try to summarize what we learned so far about operators
- Pretend your friend was absent from class today and asks you to explain the lesson. What would you tell?
- If you were writing a quiz over today's material, what are 2 questions that you would put in?
- What would you like us to review next time?