Introduction to JS
Introduction to JS
External Scripts
● Scripts can also be loaded from an external file
● This is useful if you have a complicated script
JavaScript Variables
Variable means anything that can vary. JavaScript includes variables
which hold the data value and it can be changed anytime.
After the declaration, the variable has no value (technically it has the value
of undefined).
JavaScript Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations
between numeric operands.
2. Comparison Operators
3. Logical Operators
Logical operators are used to combine two or more conditions.
JavaScript includes following logical operators.
&& && is known as AND operator. It checks
whether two operands are non-zero (0, false,
undefined, null or "" are considered as zero), if
yes then returns 1 otherwise 0.
4. Assignment Operators
Data type indicates characteristics of data. It tells the compiler whether the
data value is numeric, alphabetic, date etc., so that it can perform the
appropriate operation.
3. Array -
An array is a special type of variable, which can store multiple
values using special syntax. Every value is associated with a numeric
index starting with 0. The following figure illustrates how an array
stores values.
Var cars = [ “car”, 1, 3]
JavaScript Functions
function sum()
// code to be executed
};
sum();
function sum(){
//code
sum();
//calling a function
<function-name>();
Function Expression
Anonymous function
JavaScript allows us to define a function without any name. This
unnamed function is called a anonymous function. Anonymous
function must be assigned to a variable.
NB:
Javascript Conditions
● Use if to specify a block of code to be executed, if a specified
condition is true
● Use else to specify a block of code to be executed, if the same
condition is false
● Use else if to specify a new condition to test, if the first condition is
false
● Use switch to select one of many blocks of code to be executed
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each
time with a different value.
If Statement Example
As the most common type of conditional, the if statement only runs if
the condition enclosed in parentheses () is truthy.
EXAMPLE
if (10 > 5) {
var outcome = "if block";
}
outcome;
OUTPUT
"if block"
EXAMPLE
} else {
}
outcome;
OUTPUT
"else block"
IIn the example above, "cat" and "dog" are not equal, so the else block
runs and the variable outcome gets the value "else block".
EXAMPLE
if (false) {
} else {
outcome;
OUTPUT
"else if block"
You can use multiple if else conditionals, but note that only the first
else if block runs. JavaScript skips any remaining conditionals after it
runs the first one that passes.
EXAMPLE
if (false) {
} else if (true) {
} else if (true) {
} else {
outcome;
OUTPUT
EXAMPLE
if (false) {
outcome;
OUTPUT
JQUERY Resources
https://www.w3schools.com/jquERy/default.asp
https://jqueryui.com/