# Week 2 - Conditionals and Functions
# Object Basics
As discussed last week, when you create a variable and put something inside it, the thing that you are putting inside is either a Primitive value OR an Object.
JavaScript comes with lots of pre-built objects, but we can also create our own generic objects to hold any information we want. Here is a sample bit of JavaScript that creates an Object with four properties inside it.
let steve = {
job: 'Professor',
id: 123456,
email: 'griffis@algonquincollege.com',
alive: true,
};
2
3
4
5
6
Two of the property values hold String primitive values, one holds a number primitive value, and the last holds a Boolean primitive value.
Learn more about Object in JavaScript
# Booleans
Booleans are one of the Primitive types. They can have a value of true
or false
. The value must be written in lowercase and have no quotation marks around it. However, other values in JavaScript will also be considered either truthy
or falsy
.
# if Statements
When your program needs to make a decision and carry out different tasks based on the value of one or more variables, then an if
statement is how we do this.
They are known as Control Flow statements. Here is a simple example:
let x = 123;
if (x < 100) {
console.log('x is less than 100');
} else {
console.log('x is more than or equal to 100');
}
2
3
4
5
6
Learn more about if statements
# Ternary Statements
The ternary statement allows us to write simple logic statements similar to an if..else
statement but one one line, with less syntax. Here is a simple example of one:
let y = x < 100 ? 50 : 150;
//y will be assigned either the value 50 or the value 150
//depending on the value of x.
2
3
# Functions
In JavaScript, functions are a way to bundle together a series of commands so that they can be called all together at the same time. Here is an example of what a function declaration looks like.
function doSomething() {
let num = Math.random(); //generate a random number between 0 and 0.99999999
console.log(num);
}
doSomething(); //tell the doSomething function to run
2
3
4
5
6
# Scope and Hoisting
Scope is a term that describes the area in your code where each variable and function is visible to other parts of your code.
When variables and functions are declared, they are actually hoisted to the top of their scope. This is done before any of your code is executed.
Learn more about scope and hoisting
# Future Video Reference
When trying to find one of Steve's videos on a certain topic, go to the Channel Home page Video Tab and click on the Search icon beside the About tab. You can type in the name of the video title or part of it.
Alternatively, you can go to the playlist page The videos are grouped by topic into playlists. Eg: Arrays, DOM, Events, Functions, etc.
# Special Character Reference
Here is a list of the special characters that you will use in programming and what they are each called. Some have multiple names
: colon
; semi-colon
{ } curly braces
[ ] square-brackets
( ) parentheses
` backtick
' single quotes
" double quotes
< > angle brackets (less than, greater than)
! not operator or bang operator
& ampersand
# hashtag, pound sign, octothorpe
- dash, hyphen, subtraction operator
+ addition operator
* asterix, multiplication operator
% percentage sign, modulus operator
/ forward slash, division operator
\ back slash
_ underscore
~ tilde
, comma
. period, dot operator
? question mark
^ caret (pronounced like "Carrot")
@ at-sign
| pipe character, pipe operator
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# TODO
TODO Before next week
- If you have not read all these notes and watched the videos from this week, do that first.
- Finish and submit the exercises
Exercise 1 - variables
andExercise 2 - objects
this week, before 5pm on Tuesday. - Complete Hybrid 4 - Websites before next Monday at 5pm.
- Read ahead the notes for week 3 to prepare for class and the quiz.