Introduction to JS (JavaScript)
Last updated
Was this helpful?
Last updated
Was this helpful?
Open the Browser Dev Tools Console panel using the shortcut Command + Option + j
for MacOS or Control + Shift + j
for Linux or Windows.
Add a JavaScript file to any html file using a script
tag.
alert
console.log
Commented out lines of code will not be executed. In addition to temporarily preventing some lines of code from running, comments are also used to annotate your code.
In VSCode, you can quickly toggle comments by using the shortcut Command + /
for MacOS or Control + /
for Linux or Windows.
string
Any number of characters enclosed in backticks or single/double quotes.
number
Any numerical value (whole number or decimal), Infinity
, or NaN
boolean
The values true
and false
undefined
The value of an uninitialised variable.
null
Represents a missing object value (Objects are a complex data type that will be discussed later).
To check the type of a value, use the typeof
operator.
Variables are a way to store values for reuse later in your code or for labeling data with descriptive names.
JavaScript has three ways to create variables: const
, let
, and var
. The latter is a much older syntax and is rarely, if ever, used in modern JavaScript.
Variable Naming Rules
A variable can be made up of letters, numbers, underscores (_
), and dollar signs ($
).
It cannot start with a number.
Case sensitive
Use camelCase for multi-word variable names (not a rule per se but a strong convention)
const
vs let
Once initialised with a value const
variables cannot be reassigned while let
can.
Because const
's can't be reassigned later on, you have to always initialise it with a value. let
variables can be declared first, then initialised with a value later.
The convention in JavaScript is to use const
unless you know you need to reassign the variable at some other point in your code, in which case you use let
.
Mathematical Operators
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo
**
Exponent
Note Operator precendence is the same as in your Math class in school (PEMDAS).
String Concatenation & Interpolation
The +
operator when used with strings concatenates (or "glues") strings together.
Alternatively, you can use backtick strings (Template literals) to interpolate the variables in the string.
Template literal interpolation isn't limited to variables, it can be any expression that produces a value.
Control flow is simply applying conditional logic to how your code is run. You may want to run certain blocks of code but not others, or you might want to run certain code blocks over and over again. We probably wouldn't want to show a user account page to a user who isn't logged in, since there would be no data to display!
We're going to learn about two new forms of control flow you already know one!
The first form of control flow is: linear.
Linear control flow simply means that our data flows through our code, one line after the next. Execution starts at the first line of code, moves on to the second, then the third, and so on until the end of the file, when execution stops.
The second form of control flow is: conditional.
We will often run in to scenarios where we want one block of code to run if, and only if, a particular condition is met. Perhaps we're asking a user for their age and we want to ensure that the value they give us is a number greater than 0. Conditional execution in JavaScript is achieved with the if
statement:
In the above snippet of code, we will only see the message 'you can drive!'
if the age
variable is 16
or greater. Later, we'll explore else
and else if
- ways for us to test other conditions or run code specifically when our condition is not met. We'll dive into this pattern more in a few minutes!
The third form of control flow is: looping. We'll get into that next time.
>
Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
==
Equality
!=
Inequality
===
Strict Equality
!==
Strict Inequality
Strict vs Non-strict Equality Comparisons
It is best practice to use strict equality unless you have a very good reason to not use it. It is more likely that you will get some unexpected results with using non-strict equality comparisons. Compare the equality tables of both and you'll see that strict comparisons are more straightforward and predictable.
&&
AND
||
OR
Conditionals are a common feature of programming languages because it's handy to execute different blocks of code depending on whether a condition is met.
Conditionals follow this pattern:
The above is a simple conditional statement, with only a single condition. We can add a negative condition (code that will run if the condition is false
) and we can add additional positive conditions.
If we have code we want to run if our condition is false, we use else
:
If we want to test multiple conditions, we can do so by combining else
and if
:
We can add as many else
/if
statements as we want, one for each condition we want to check, but after a while our code becomes hard to read:
If you want to test two conditions without using and else
/if
you can do so by using the logical operators (&&
and ||
):
We can also add nested if/else statements:
There's no (practical) limit to the depth of if / else statements. But you can imagine that this gets difficult to read and understand fairly quickly. Simpler is better!
We'll dive deeper into this topic in the next few weeks. For now, here's a quick code snippet that shows you how to manipulate the content in a webpage "live".