You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.0 KiB
3.0 KiB
Intro to JS
Lesson Objectives
- Add comments to code
- Describe the basic data types of JS
- Assign values to variables
- Concatenate values
- Boolean expressions
- Operators
- Write a While loop
- Write a For loop
Add comments to code
single line comment:
// this is a single line comment
multi-line comment:
/*
this is
a mult-line
comment
*/
Describe the basic data types of JS
Strings:
console.log("hello world");
Numbers:
console.log(100);
Assign values to variables
var phrase = 'In my room is a chair and a table';
console.log(phrase);
We will not be using var (it's a bit out of date), instead we will be using let and const.
Let's use const here:
const phrase = 'In my room is a chair and a table';
const sum = 99 + 1;
const variables are constant and do not change. Now that the phrase is saved to a variable, we can call it whenever.
console.log(phrase);
console.log(sum);
Variable re-assignment
With const, you cannot reassign a variable. It is CONSTANT.
const item = ' chair';
item = 'eclair';
You can re-assign variables with let:
let item = 'chair';
item = 'eclair';
console.log(item);
Concatenate values
JavaScript allows us to add strings together with +:
console.log('hello' + ' world');
We can insert values of variables into our strings:
const adjective = 'beautiful';
console.log('What a ' + adjective + ' day!!');
Boolean expressions
Test various kinds of equality
>greater than<less than==equal to>=greater than or equal to<=less than or equal to!=is not equal
console.log(5 <= 10);
console.log("asdf" != "fun");
Operators
Math
We can do arithmetic with numbers:
console.log(100 + 100);
console.log(100 - 50);
console.log(100 * 10);
console.log(100 / 10);
Postfix operator
i++;
i--;
is the same as
i = i + 1;
i = i - 1;
Compound assignment operator +=
i += 3; //increments by 3
i -= 3; //decrement by 3
is the same as
i = i + 3;
i = i - 3;
Write a While loop
let num = 1;
while (num <= 1000) {
console.log('The number is: ' + num);
num++;
}
Write a For loop
for (let i=1; i <= 1000; i++) {
console.log('The number is: ' + i);
}
There are three parts to the 'control panel' of the loop, delimited by the semicolon:
for (initial condition; while condition; repeating expression) {
}
- some initial code supplied to the loop -- usually a numerical starting value of the loop
- the condition under which the loop runs -- it will run while the expression is true
- a repeating expression that runs at the end of each loop -- usually an instruction to increase the numerical starting value