Introduction To JavaScript
Introduction To JavaScript
Understanding
Basic Syntax
Objectives
04 Operators Objective: Know various Operators in JavaScript and how to use them
JavaScript is:
▪ cross-platform
▪ object-oriented
▪ client-side
▪ scripting language
JavaScript was developed
▪ Used to make webpages interactive:
by Brendan Eich in 1995
• having complex animations
• clickable buttons
• popup menus etc.
▪ JavaScript -
to program the behavior of web pages
▪ Document type
<!DOCTYPE html> this is a HTML5 type
of document
▪ Tags
▪ Elements
• Root element <html>
• Contains meta info <head>
• Contains visible contents <body>
▪ id of an Element (unique attribute for an
HTML element)
Where to Insert JS Code in HTML?
Inserting JS code in HTML
▪ Script element
<script> </script>
▪ Within the <head> element
▪ Within the <body> element
▪ At the bottom of the <body> element
▪ As an attribute
How to Check the Results (Output)?
Various JS output display options
Statements:
// Statements end with a ; delimiter
▪ Statements are separated by ; delimiter
var x, y, z; // Statement to Declare Variables
▪ Single instruction unit of JS programming
▪ One-line multiple statements & multi-line
// Multiple statements can be in one line
statements
▪ Multiple blank spaces are ignored x = 1; y = 2; // Statement to Assign value
Values:
// variables are used to store values
▪ Literals (fixed values)
var x, y, z; // Declare Variables
• Numbers
(with decimals, without decimals,
// Literal (Number) without decimals
exponent)
x = 1; // Assign Literal (Number) to Variable
• Strings
(single quoted, double quoted) // Literal (Number) with a decimal
Operators:
var x, y, z;
▪ Arithmetic operators
x = 1; // Assign
( + - * / % ** ++ -- ) to compute values
▪ Assignment operators
y = 2; // Assign
var a = 'Java'; // Assign
( = += *= %= **= ) to assign values to
variables
▪ Comparison operators // Operators and Operands
Arithmetic Operators:
var x, y;
▪ Simple arithmetic operators
x = 5; // Assign
( + - * / ) add, subtract, multiply, divide
▪ Modulus operator
y = 2; // Assign
var a = x + y; // adds numbers, 7
( % ) to compute the division remainder
▪ Increment & Decrement operators var b = x - y; // subtracts numbers, 3
Assignment Operators:
var x;
▪ Simple assignment operator
// Assign value to a variable
( = ) Assign value to a variable
▪ Add assignment operator x = 3; // value of x returns 3
// Assign added value
( += ) Assign added value to a variable
▪ Subtract assignment operator x += 2; // value of x returns 5
Comparison Operators:
var x, age = 19;
▪ Equal to operator
if (age > 18) x = "Adult"; // Adult
( == )
▪ Equal value and equal type operator
(age > 18) // true
( === )
▪ Not equal operator age = 12;
▪ Not equal value or not equal type operator (age == "12") // true
( !== ) (age === "12") // false
▪ Greater than & less than operator (age === 12) // true
(><) (age != "10") // true
▪ Greater than or / less than or equal to (age !== 10) // true
operator
( >= <= ) age = 18;
(age >= 18) // true
Conditional Operators
It assigns a value to a variable based on some condition
age = 18;
x = (age >= 18) ? "Adult":"Minor"; // Adult
(age >= 18) // true
Logical Operators
Logical Operators: To Check Logic between Values or Variables
Logical Operators:
var x, y;
▪ Logical and operator
( && )
▪ Logical or operator
x = 5;
y = 3;
( || )
▪ Logical not operator
(!) // Logical and operator
(x < 8 && y > 1) // true
// Logical or operator
(x == 10 || y < 1) // false
Expressions:
Any unit of code that can be evaluated var x = 1, y = 4;
to a value. // Primary Expressions
'Javascript'; // A string literal
▪ Primary expressions
54.2; // A numeric literal
string / numeric literals, boolean,
true; // Boolean value true
variables
sum; // Value of variable sum
('hello world', 54.2, true, x, this)
this; // A current object
▪ Arithmetic expressions
Arithmetic expressions evaluate to a
// Arithmetic Expressions
numeric value
x + 2;
▪ String expressions
// String Expressions
String expressions evaluate to a string
▪ Logical expressions
"Java" + "script";
// Logical Expressions
logical expressions evaluate to the
boolean value x < 8 && y > 1;
(true, false)
Comments in JS
Written part in the code that are ignored by the JavaScript engine
Comments:
// Declare Variables (line comment)
Are used to add information, warnings or
var x, y, z; // Var declired (line comment)
suggestions about the code, making a code easy
/* this is a block comment */
to interpret
x = 1;
Identifiers:
// Allowed Identifiers
Name of a variable, function, or property
abc; // starts with a letter
▪ Rules for legal identifiers
_abc; // starts with a underscore
1st character: a letter, underscore (_), dollar sign
$abc; // starts with a dollar sign
($)
Subsequent characters may be letters, digits, _ , $ অলীক; // starts with Unicode character
console.log(str.length);
Strings in JavaScript
Strings are used for storing and manipulating text
Numbers Methods:
// convert to String,Exponential,Num of digits
var b = 2.5
▪ toExponential: returns string in
console.log(b.toString());
exponential notation
▪ toFixed(n): returns string with n number of
console.log(b.toExponential());
console.log(b.toFixed(2));
decimals
▪ toPrecision(n): returns string, with length n console.log(b.toFixed(0));
▪ if Statement
speakOut = "It's over a Dozen";
numOfItems += 2;
if (condition) {
// executed if condition is true }
}
▪ else Statement if(numOfItems >= 12){
if (condition) { speakOut = "It's over a Dozen";
// executed if condition is true numOfItems += 2;
} else { } else {
// executed if condition is false speakOut = "It's less than a Dozen";
} }
Conditional Statements
Decisions making: else if
▪ else if Statement
if(numOfItems == dozen){
speakOut = "It's a Dozen";
if (condition 1) {
// executed if condition is true totalOffer = offerPerDozen;
▪ switch Statement
break;
case "winter":
switch(expression) {
case x: console.log('It\'s cold!');
▪ for loops:
for (let i = 0; i < 10; i++) {
for (statement 1; statement 2; statement 3)
sum += i;
{
// code block to be executed }
}
▪ statement 1: console.log(sum);
sets a variable before the loop starts
▪ statement 2:
defines condition for the loop to run
▪ statement 3:
increment the initial variable
Loops in JavaScript
Loops: Loops can execute a block of code a number of times
} }
console.log(txt);
▪ do/while Loops : // do-while loop
check condition after the loop do {
do { txt += x + " ";
// code block to be executed x++;
} } while (x < 10);
while (condition); console.log(txt);
Jumps in Loops
Jump out of a loop & Jump over one iteration in a loop
} }
if (i == 5) {
▪ continue : Jump over one iteration in a loop break;
if condition is true }
loop { indices += i + ",";
if (condition) { continue; } }
}
console.log(indices);
Recap
Understanding Basic Syntax