JavaScript Notes
JavaScript Notes
Version 1 - 15/04/2022
Prepared by Mudassar Islam for Students in the Web Development Course
_____________________________________________________________________
—------------------------------------------------------------------------
Introduction
—------------------------------------------------------------------------
JavaScript is the language of the web browser. It is used alongside HTML and
CSS to make websites more useful. JavaScript is used to add special
functionality to your websites. A few applications may include:
● Dropdown Menus
● Accordions
● Sliders
● Image Galleries
● Form Validation
● Show / Hide Elements
● Animations
—------------------------------------------------------------------------
Syntax of JavaScript
—------------------------------------------------------------------------
JavaScript follows the C family syntax. Below is a basic example of this syntax.
The syntax of JavaScript has some important properties:
Variables in programming are used to store a value so that we can use it for
any purpose later on.
For example, we may take user input for a number, and show them the
Multiplication Table of that number. To do this we must store the number
entered by the user in our code. We can use Variables to do this.
Variables in JavaScript are declared using the “let” keyword. Variables have
some properties that you need to remember:
Constants are used to store data that will not change. For example the value
of Pi, The number of items in a dozen. This data can never change So it can be
stored as a constant.
However, for data that will change, You must use Variables. For example,
The time of day. The time of day changes every passing second, So it cannot
be stored in a constant.
—------------------------------------------------------------------------
Data Types in JavaScript
—------------------------------------------------------------------------
JavaScript variables are used to store any kind of data. This data stored inside
variables is represented by Data-Types. JavaScript has the following
data-types:
● Numbers
● Strings
● Booleans
● Arrays
■
■ Key value pairs are also called Associative Array or Hash Map
in other programming languages.
—------------------------------------------------------------------------
Operators in JavaScript
—------------------------------------------------------------------------
● Arithmetic Operators
○ Plus ( + )
■ The Plus operator is used to add numbers to each other.
● i.e. 12 + 15, variable1 + variable2.
■ The plus operator can also be used with strings to combine
them together.
● i.e. “My name is “ + “Mudassar” will become “My
name is Mudassar”
■ Plus is the only operator that can be used with both strings
and numbers.
○ Minus ( - )
■ The minus operator is used to subtract one value from
another.
● i.e. 8 - 5, variable1 - variable2;
■ It can only be used on number-like values.
○ Multiply ( * )
■ The multiply sign is the asterisk ( * ).
■ It is used to multiply values to each other.
● i.e. 8 * 16, variable1 * variable2.
■ It can only be used on number-like values.
○ Divide ( / )
■ The divide sign is the forward slash ( / ).
■ It is used to divide one value by another.
● i.e. 5 / 2, variable1 / variable2.
■ It can only be used on number-like values.
○ Modulus ( % )
■ The modulus sign is the percentage symbol ( % ).
■ It is used to take the modulus of a value by another.
● Modulus just means division but the remainder is
returned instead of decimal value.
○ Modulus of 4 % 2 is 0, Whereas 5 % 2 is 1
● i.e. 7 % 2, variable1 % variable2.
■ It is only used on number-like values.
○ Exponent ( ** )
■ The exponent sign is double-asterisk ( ** ).
■ Exponent means raising the power of a number by another.
■ It is used to raise a number to any power.
● i.e. 8**2 (8 squared), variable1**variable2.
■ It is only used on number-like values
○ Not Equal To ( != )
■ The symbol is the != sign.
■ Checks whether a value is not equal to another value.
■ i.e. 7 != 4, variable1 != variable2
● Bitwise Operators - They are used to combine multiple conditions
together. They can also be used to invert the result of a condition.
These operations represent the logic gates in mathematics.
○ AND ( && )
■ The symbol of the and operator is double-ampersand ( && ).
● The ampersand symbol is Shift + 7 on your keyboard.
■ This operator ONLY evaluates when the result of 2 or more
conditions is TRUE.
■ i.e. 8 > 5 && 6 < 4
● Will only evaluate if both conditions are True.
○ OR ( || )
■ The symbol of the and operator is double-pipe ( || ).
● The pipe symbol is under the backspace key on your
keyboard. You need to press Shift + Backspace to get
the pipe.
■ This operator evaluates even when only 1 condition from
multiple is true.
● If you have 5 conditions, and 4 of them are false but
one of them is true, This operator will evaluate.
■ i.e. 8 > 5 || 6 < 4
● Will evaluate as long as at-least one condition is true.
○ NOT ( ! )
■ The symbol of the and operator is exclamation ( ! ).
● The exclamation symbol is Shift + 1.
■ This operator INVERTS the result of any given condition.
● For example if you have a condition that is true such as
8 > 5, You can invert it’s result by !(8 > 5).
● If a condition is true, the not symbol will turn it to false.
● If a condition is false, the not symbol will turn it to true.
—------------------------------------------------------------------------
Functions in JavaScript
—------------------------------------------------------------------------
● Each function starts with its declaration with the “function” keyword.
● Then you write the function name which follows the same rules as the
variable naming scheme.
● After the function name, you add brackets ().
○ You can specify any parameters for that function in those brackets.
○ Parameters are just variables inside the function.
○ You don’t have to declare parameters with the “let” keyword.
They are declared automatically.
○ Parameters are used to work with dynamic values inside your
function.
○ When a function is called, we can specify any value as the
parameter.
● After the brackets, we start a code block with “{“ and “}”.
● Inside this code block, we write all the code of the function body.
○ This code will not be executed automatically.
○ We need to call the function to execute this code in our
application.
● A function may or may not have a return value.
○ A function without a return value is called a Void Function.
○ A function with a return value can be used in variables and if
statements to assign that value.
○
●
You can also make functions that have no name. These are usually used with
Events and callbacks. For example, when a button is clicked. We may want to
call a function, but we don’t have to give this function a name because It only
be called by the button and never again in the application.
Unnamed function declaration is the same but You skip the function name.
—------------------------------------------------------------------------
If, Else If, Else in JavaScript
—------------------------------------------------------------------------
If, Else if, Else or If statements for short, are used to evaluate conditions in
JavaScript. For example, You may want to check if a student passed an exam,
or If a user’s password is correct before logging them in.
We use If statements to do this in JavaScript. Any time you want to check a
condition, You must use an if statement.
A Loop is a code block in JavaScript that repeats the same action over and
over again. If you want to perform the same action in your code over and
over again in a sequence, You can use a Loop to do that.
● For Loop
○ This loop runs for a specific amount of iterations and uses a loop
variable.
○ This is the most common and important type of loop.
● While Loop
○ This loop runs as long as a given condition matches.
○ While loop is not used as much as for loop. If you want to learn
about the while loop, Refer to W3Schools.
● After the loop information, You can start a code block with “{“ and “}”.
Any code within this block will keep repeating over and over again.
Always remember that the Loop Variable is accessible in the Loop Body.
—------------------------------------------------------------------------
Concluding Messages & Project Ideas
—------------------------------------------------------------------------
These notes only cover the fundamentals of JavaScript. You need to have a
very strong grip of the fundamentals in order to work with the language.
Here’s a few small project ideas to practice these fundamentals. These are
both advanced and beginner topics so choose one you feel is good for you.