COMP 1537 - Week 4 - JavaScript Basics1
COMP 1537 - Week 4 - JavaScript Basics1
Developer tools:
Elements – highlights HTML elements within the document
Console – displays info/warnings/errors (very important to use this!)
Sources – the different places where resources come from (e.g., ad networks)
Memory – memory usage of your app (includes JavaScript, connections, etc.)
Network – displays all data send/received
Performance – shows info on memory usage/function calls, etc.
When programming
You must (strictly) adhere to the language syntax
Or you will receive syntax errors in your program
Operators:
Several types of operators:
Assignment, arithmetic, comparison, bitwise, logical, string, special
Assignment operators:
Assign a value from right operand to left operand
var x = 4;
Arithmetic operators:
+, -, /, *
var x = (4 + 3);
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
Comparison operators
Comparing things to other things is fundamental in programming – JavaScript being no
exception
Comparing and acting on comparing utilizes two tools:
Equality/relational/logical operators
Conditional statement/(ternary)operator/switch
Logical operators
Check logical conditions
AND: all conditions must be met
E.g., it is sunny AND it Friday
OR: any one of the conditions must be met
E.g., It is Friday or it is Saturday
String operator:
'+'
Looks like the plus operator for arithmetic
What's the difference?
None between the symbol, difference is between the data types
We call this operator overloading
Meaning an operator is used in different contexts – and behaves differently based on the type
What happens when we mix data types?
A loop that executes a specified statement as long as the test condition evaluates
to true:
while (condition) {
statement
}
Condition: the expression that is evaluated at the end of each loop
Statement: one or more statements that get executed each iteration
As long as the final expression evaluates to true
Creates a loop that executes a specified statement until the test condition
evaluates to false:
do
statement
while (condition);
Condition: the expression that is evaluated at the end of each loop
Statement: one or more statements that get executed each iteration
As long as the final expression evaluates to true
For loop:
The "all inclusive" loop
i.e., initialization, condition, final expression
May not execute at all – depending on conditions
For in loop:
Used to iterate over the enumerable properties
The property order is arbitrary (i.e., not significant)
While loop
Requires variables and expressions to be made outside of loop
May not execute at all – depending on conditions
Do while loop
Requires variables and expressions to be made outside of loop
Executes at least once
For loop:
When you need fine control over conditions/incrementing/initialization
For in loop:
When going over a set of things in an object
While loop:
When you have an indefinite number of iterations
E.g., reading lines of text from a file
Do while loop:
When performing input checking
where loop body needs to be executed atleast once.
https://developer.mozilla.org/en-
US/docs/Learn/Getting_started_with_the_web/JavaScript_basics
https://javascript.info/