JavaScript_Basics(constantly updated)
A flexible language that powers dynamic websites and apps
Variables
Dynamic websites need to remember information to display or change. For that, JavaScript has variables
Like moving boxes, variables have content and names that tell us what’s inside
Variable types
- number:
- variable type that stores number
- string:
- variable type that stores a series of char
- can be recognized with the two double quotes, between which any symbol can stay
- Eg:
"ajjef91239mca dsjfjja03"
- boolean:
- stores the result of comparison
true
false
var thisIsAVariable
var name="Samuel";
- variable name - name
- Variable value - “Samuel”
- “Samuel” is a string
- A statement ends with semicolon
;
var time = 5;
- 5 is a number, not a string : since there’s no “ “
var lighton = true
var finishedHW = false
Instruction
With special instruction console.log()
, we tell the computer to display a value in an area called the console.
console.log("Hello World");
When we display variables in the console, their values appear instead of their names. If we logname
here, it’ll show its value.
Arithmetic operations are supported with numbers:
+
plus-
minus*
multiply/
divides
joining operator: +
+ when +
is used between two numbers, the result will be their sum
+ when +
is used between two strings, the result will be the joining of the string
+ "name:" + " Samuel"
-> "name: Samuel"
+ when +
is used between a string and a number, the number will be automatically transformed into a string
+ "age: " + 8
-> "age: 8"
+ when +
is used between a string and a boolean, the boolean will be automatically transformed into a string
+ "Passed": + true
-> "passed: true"
Using Variables
Variables are called variables because the values they store can change
Use the =
sign to change the value in status
from "Watching Netflix"
to "Playing frisbee"
1
2
3
var currentStatus = "Watching Netflix";
currentStatus = "Playing frisbee";
console.log(currentStatus);
1 | var currentStatus = "Watching Netflix"; |
Comparing operators
All comparison statements returns a boolean value
===
- this is used to check whether two variables are equal
- variables can be of type
string
ornumber
- variables can be of type
1===2
returnsfalse
"apple"==="apple"
returnstrue
- this is used to check whether two variables are equal
!==
- this is used to check whether two variables are not equal
1!==2
returnstrue
>
- this is used to check whether a variable is larger than another variable
1>2
returnsfalse
<
- this is used to check whether a variable is smaller than another variable
1<2
returnstrue
<=
- this is used to check whether a variable is smaller or equal than another variable
1<=2
returnstrue
1<=1
returnstrue
>=
this is used to check whether a variable is larger or equal than another variable1>=2
returnsfalse
If/else statement
How can we write code that adapts to different situations? By using the if statement.
Structure:
1 | if(condition){//place a boolean value in the bracket |
Properties:
- The if statement runs code only if the boolean it’s relying on is
true
. It’s like saying, of something is true, then do this.
- We use
false
instead to skip the code inside the curly braces. - The
else
statement doesn’t need its own condition. That’s because it handles the cases where the if’s conditionfalse
Definitions:
Values like
true
that are placed inside the parenthesis are called conditions.- These can be:
true
false
- comparison of variables
a === b
a !=== b
a < b
a <= b
a > b
a >= b
Statements relying on conditions are called conditionals.
Incorporating else if
1 | var age = 10; |
Properties:
- As long as they go before the
else
statement, we can add as manyelse if
statement as we’d like