SlideShare a Scribd company logo
www.webstackacademy.com ‹#›
Types and
Statements
JavaScript
www.webstackacademy.com ‹#›

Reserve Keywords

Data Types

Statements
Table of Content
www.webstackacademy.com ‹#›www.webstackacademy.com ‹#›
Reserve Keywords
(JavaScript)
www.webstackacademy.com ‹#›
JS - Reserved Words
●
Any language has a set of words called vocabulary
●
JavaScript also has a set of keywords with special purpose
●
These special purpose keywords words are used to construct
statements as per language syntax and cannot be used as
JavaScript variables, functions, methods or object names
●
Therefore, also called reserve keywords
●
The limited set of keywords help us to write unlimited statements
www.webstackacademy.com ‹#›
Reserved Words
abstract debugger final instanceof protected throws
boolean default finally int public transient
break delete float interface return true
byte do for let short try
case double function long static typeof
catch else goto native super var
char enum if new switch void
class export implements null synchronized volatile
const extend import package this while
continue false in private throw with
*keywords in red color are removed from ECMA script 5/6
www.webstackacademy.com ‹#›www.webstackacademy.com ‹#›
Data Types
(JavaScript)
www.webstackacademy.com ‹#›
JS - Variables
●
Variables are container to store values
●
Name must start with
– A letter (a to z or A to Z)
– Underscore( _ )
– Or dollar( $ ) sign
●
After first letter we can use digits (0 to 9)
– Example: x1, y2, ball25, a2b
www.webstackacademy.com ‹#›
JS - Variables
●
JavaScript variables are case sensitive, for example
‘sum’ and ‘Sum’ and ‘SUM’ are different variables
Example :
var x = 6;
var y = 7;
var z = x+y;
www.webstackacademy.com ‹#›
JS - Data Types
●
There are two types of Data Types in JavaScript
– Primitive data type
– Non-primitive (reference) data type
Note : JavaScript is weakly typed. Every JavaScript variable has a data type , that type can change dynamically
www.webstackacademy.com ‹#›
Primitive data type
●
String
●
Number
●
Boolean
●
Null
●
Undefined
www.webstackacademy.com ‹#›
Primitive data type
(String)
●
String - A series of characters enclosed in quotation
marks either single quotation marks ( ' ) or double
quotation marks ( " )
Example :
var name = “Webstack Academy”;
var name = 'Webstack Academy';
www.webstackacademy.com ‹#›
Primitive data type
(Number)
●
All numbers are represented in IEEE 754-1985 double
precision floating point format (64 bit)
●
All integers can be represented in -253 to +253 range
●
Largest floating point magnitude can be ± 1.7976x10308
●
Smallest floating point magnitude can be ± 2.2250x10-308
●
If number exceeds the floating point range, its value will be
infinite
www.webstackacademy.com ‹#›
●
Floating point number is a formulaic representation which
approximates a real number
●
The most popular code for representing real numbers is called
the IEEE Floating-Point Standard
Sign Exponent Mantissa
Float (32 bits) Single Precision 1 bit 8 bits 23 bits
Double (64 bits) Double Precision 1 bit 11 bits 52 bits
Primitive data type
(Number)
Float : V = (-1)s * 2(E-127) * 1.F
Double : V = (-1)s * 2(E-1023) * 1.F
www.webstackacademy.com ‹#›
Primitive data type
(Number formats)
●
The integers can be represented in decimal,
hexadecimal, octal or binary
Example :
var a = 0x10; // Hexadecimal
var b = 010; // Octal number
var c = 0b10; // Binary
var num1 = 5; // Decimal
var num2 = -4.56; // Floating point
www.webstackacademy.com ‹#›
Primitive data type
(Number conversion)
●
Converting from string
Example :
var num1 = 0, num2 = 0;
// converting string to number
num1 = Number("35");
num2 = Number.parseInt(“237”);
www.webstackacademy.com ‹#›
Primitive data type
(Number conversion)
●
Converting to string
Example :
var str = “”; // Empty string
var num1 = 125;
// converting number to string
str = num1.toString();
www.webstackacademy.com ‹#›
Primitive data type
(Number – special values)
Special Value Cause Comparison
Infinity, -Infinity Number too large or
too small to represent
All infinity values compare equal to
each other
NaN (not-a-
number)
Undefined operation NaN never compare equal to
anything (even itself)
www.webstackacademy.com ‹#›
Primitive data type
(Number – special value checking)
Method Description
isNaN(number) To test if number is NaN
isFinite(number) To test if number is finite
www.webstackacademy.com ‹#›
Primitive data type
(Boolean)
●
Boolean data type is a logical true or false
Example :
var ans = true;
www.webstackacademy.com ‹#›
Primitive data type
(Null)
Example :
var num = null; // value is null but still type is an object.
●
In JavaScript the data type of null is an object
●
The null means empty value or nothing
www.webstackacademy.com ‹#›
Primitive data type
(Undefined)
Example :
var num; // undefined
●
A variable without a value is undefined
●
The type is object
www.webstackacademy.com ‹#›
Non-primitive data types
●
Array
●
Object
www.webstackacademy.com ‹#›
Arrays
●
Array represents group of similar values
●
Array items are separated by commas
●
Array can be declared as :
– var fruits = [“Apple” , “Mango”, “Orange”];
www.webstackacademy.com ‹#›
Objects
●
An Object is logically a collection of properties
●
Objects represents instance through which we can access
members
●
Object properties are written as name:value pairs separated by
comma
Example :
var student={ Name:“Mac”, City:“Banglore”, State:“Karnataka”};
www.webstackacademy.com ‹#›www.webstackacademy.com ‹#›
Statements
(JavaScript)
www.webstackacademy.com ‹#›
JS – Simple Statements
●
In JavaScript statements are instructions to be executed
by web browser (JavaScript core)
Example :
<script>
var y = 4, z = 7; // statement
var x = y + z; // statement
document.write("x = " + x);
</script>
www.webstackacademy.com ‹#›
JS – Compound Statements
Example :
<script>
...
if (num1 > num2) {
if (num1 > num3) {
document.write("Hello");
}
else {
document.write("World");
}
}
...
</script>
www.webstackacademy.com ‹#›
JS – Conditional Construct
Conditional
Constructs
Iteration
Selection
for
do while
while
if and its family
switch case
www.webstackacademy.com ‹#›
JS – Statements
(conditional - if)
Syntax :
if (condition) {
statement(s);
}
Example :
<script>
var num = 2;
if (num < 5) {
document.write("num < 5");
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(conditional : if-else)
Syntax :
if (condition) {
statement(s);
}
else {
statement(s);
}
www.webstackacademy.com ‹#›
JS – Statements
(conditional : if-else)
Example :
<script>
var num = 2;
if (num < 5) {
document.write("num is smaller than 5");
}
else {
document.write("num is greater than 5");
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(conditional : if-else if)
Syntax :
if (condition1) {
statement(s);
}
else if (condition2) {
statement(s);
}
else {
statement(s);
}
www.webstackacademy.com ‹#›
JS – Statements
(conditional : if-else if)
Example :
<script>
var num = 2;
if (num < 5) {
document.write("num is smaller than 5");
}
else if (num > 5) {
document.write("num is greater than 5");
}
else {
document.write("num is equal to 5");
}
</script>
www.webstackacademy.com ‹#›
JavaScript - Input
Method Description
prompt() It will asks the visitor to input Some information and
stores the information in a variable
confirm() Displays dialog box with two buttons ok and cancel
www.webstackacademy.com ‹#›
Example - confirm()
Example :
<script>
var x = confirm('Do you want to continue?');
if (x == true) {
alert("You have clicked on Ok Button.");
}
else {
alert("You have clicked on Cancel Button.");
}
</script>
www.webstackacademy.com ‹#›
Example - prompt()
Example :
<script>
var person = prompt("Please enter your name", "");
if (person != null) {
document.write("Hello " + person +
"! How are you today?");
}
</script>
www.webstackacademy.com ‹#›
Exercise
●
Write a JavaScript program to input Name, Address,
Phone Number and city using prompt() and display them
●
Write a JavaScript program to find area and perimeter of
rectangle
●
Write a JavaScript program to find simple interest
– Total Amount = principle * (1 + r * t)
www.webstackacademy.com ‹#›
Class Work
●
WAP to find the max of two numbers
●
WAP to print the grade for a given percentage
●
WAP to find the greatest of given 3 numbers
●
WAP to find the middle number (by value) of given 3
numbers
www.webstackacademy.com ‹#›
JS – Statements
(switch)
Syntax :
switch (expression) {
case exp1:
statement(s);
break;
case exp2:
statement(s);
break;
default:
statement(s);
}
expr
code
true
false
case1? break
case2?
default
code break
code break
true
false
www.webstackacademy.com ‹#›
JS – Statements
(switch)
<script>
var num = Number(prompt("Enter the number!", ""));
switch(num) {
case 10 : document.write("You have entered 10");
break;
case 20 : document.write("You have entered 20");
break;
default : document.write("Try again");
}
</script>
www.webstackacademy.com ‹#›
Class work
●
Write a simple calculator program
– Ask user to enter two numbers
– Ask user to enter operation (+, -, * or /)
– Perform operation and print result
www.webstackacademy.com ‹#›
JS – Statements
(while)
Syntax:
while (condition)
{
statement(s);
}
●
Controls the loop.
●
Evaluated before each
execution of loop body
false
true
cond?
code
www.webstackacademy.com ‹#›
JS – Statements
(while)
Example:
<script>
var iter = 0;
while(iter < 5)
{
document.write("Looped " + iter + " times <br>");
iter = iter + 1;
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(do - while)
Syntax:
do {
statement(s);
} while (condition);
●
Controls the loop.
●
Evaluated after each
execution of loop body
false
true
cond?
code
www.webstackacademy.com ‹#›
JS – Statements
(do-while)
Example:
<script>
var iter = 0;
do {
document.write("Looped " + iter + " times <br>");
iter = iter + 1;
} while ( iter < 5 );
</script>
www.webstackacademy.com ‹#›
JS – Statements
(for loop)
Syntax:
for (init-exp; loop-condition; post-eval-exp) {
statement(s);
}; ●
Controls the loop.
●
Evaluated before each
execution of loop body
false
true
cond?
code
www.webstackacademy.com ‹#›
JS – Statements
(for loop)
Execution path:
for (init-exp; loop-condition; post-eval-exp) {
statement(s);
};
1 2
3
4
www.webstackacademy.com ‹#›
JS – Statements
(for loop)
Example:
<script>
for (var iter = 0; iter < 5; iter = iter + 1) {
document.write("Looped " + iter + " times <br>");
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(for-in loop)
●
“for-in” loop is used to iterate over enumerable
properties of an object
Syntax:
for (variable in object) {
statement(s);
}
www.webstackacademy.com ‹#›
JS – Statements
(for-in loop)
Example:
<script>
var person = { firstName:”Rajani”, lastName:”Kanth”,
profession:”Actor” };
for (var x in person) {
document.write(person[x] + “ “);
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(for-in loop)
●
loop only iterates over enumerable properties
●
“for-in” loop iterates over the properties of an object in an
arbitrary order
●
“for-in” should not be used to iterate over an Array where
the index order is important
www.webstackacademy.com ‹#›
JS – Statements
(for-of loop)
●
“for-of” loop is used to iterate over iterate-able object
●
“for-of” loop is not part of ECMA script
Syntax:
for (variable of object) {
statement(s);
}
www.webstackacademy.com ‹#›
JS – Statements
(for-of loop)
Example:
<script>
var array = [1, 2, 3, 4, 5];
for (var x of array) {
document.write(x + “ “);
}
</script>
www.webstackacademy.com ‹#›
Class Work
●
W.A.P to print the power of two series using for loop
– 21, 22, 23, 24, 25 ...
●
W.A.P to print the power of N series using Loops
– N1, N2, N3, N4, N5 ...
●
W.A.P to multiply 2 numbers without multiplication operator
●
W.A.P to check whether a number is palindrome or not
www.webstackacademy.com ‹#›
Class Work - Pattern
●
Read total (n) number of pattern chars in a line (number
should be “odd”)
●
Read number (m) of pattern char to be printed in the
middle of line (“odd” number)
●
Print the line with two different pattern chars
●
Example – Let's say two types of pattern chars '$' and
'*' to be printed in a line. Total number of chars to be
printed in a line are 9. Three '*' to be printed in middle of
line.
●
Output ==> $$$* * *$$$
www.webstackacademy.com ‹#›
Class Work - Pattern
●
Based on previous example print following pyramid
*
* * *
* * * * *
* * * * * * *
www.webstackacademy.com ‹#›
Class Work - Pattern
●
Based on previous example print following rhombus
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
www.webstackacademy.com ‹#›
JS – Statements
(break)
●
A break statement shall appear only in “switch body” or “loop
body”
●
“break” is used to exit the loop, the statements appearing after
break in the loop will be skipped
●
“break” without label exits/‘jumps out of’ containing loop
●
“break” with label reference jumps out of any block
www.webstackacademy.com ‹#›
JS – Statements
(break)
Syntax:
while (condition) {
conditional statement
break;
}
false
true
loop
cond?
code block
cond?
false
break?
true
www.webstackacademy.com ‹#›
JS – Statements
(break)
<script>
for (var iter = 0; iter < 10; iter = iter + 1) {
if (iter == 5) {
break;
}
document.write("<br>iter = " + iter);
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(break with label)
Syntax:
outer_loop:
for (condition) {
inner_loop:
for(condition) {
conditional statement
break outer_loop; // jump out of outer_loop
}
}
www.webstackacademy.com ‹#›
JS – Statements
(continue)
●
A continue statement causes a jump to the loop-continuation
portion, that is, to the end of the loop body
●
The execution of code appearing after the continue will be
skipped
●
Can be used in any type of multi iteration loop
www.webstackacademy.com ‹#›
JS – Statements
(continue)
Syntax:
while (condition) {
conditional statement
continue;
}
false
true
loop
cond?
code block
cond?
false
continue?
true
code block
www.webstackacademy.com ‹#›
JS – Statements
(continue)
<script>
for (var iter = 0; iter < 10; iter = iter + 1) {
if (iter == 5) {
continue;
}
document.write("<br>iter = " + iter);
}
</script>
www.webstackacademy.com ‹#›
JS – Statements
(continue with label)
Syntax:
outer_loop:
for (condition) {
inner_loop:
for(condition) {
conditional statement
continue outer_loop; // continue from outer_loop
}
}
www.webstackacademy.com ‹#›
JS – Statements
(goto)
●
This keyword has been removed from ECMA script 5/6
●
“goto” keyword is is not recommended to use
●
Use break with label if necessary
www.webstackacademy.com ‹#›
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com

More Related Content

PDF
JavaScript - Chapter 3 - Introduction
PDF
JavaScript - Chapter 8 - Objects
PDF
JavaScript - Chapter 7 - Advanced Functions
PDF
JavaScript - Chapter 5 - Operators
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
PDF
JavaScript - Chapter 15 - Debugging Techniques
PDF
JavaScript - Chapter 6 - Basic Functions
PDF
JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 8 - Objects
JavaScript - Chapter 7 - Advanced Functions
JavaScript - Chapter 5 - Operators
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 10 - Strings and Arrays

What's hot (20)

PPSX
Javascript variables and datatypes
PPT
Introduction to Javascript
PPTX
Javascript functions
PPTX
Javascript 101
PPTX
Introduction to react_js
PDF
3. Java Script
PDF
Angular - Chapter 7 - HTTP Services
PPT
Javascript
PPTX
Javascript
PDF
ReactJS presentation
PPTX
JavaScript Conditional Statements
PPT
Java Script ppt
PDF
JavaScript: Variables and Functions
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
PPTX
Java 8 presentation
PDF
JavaScript Programming
PDF
Php introduction
PPTX
Node.js Express
PPTX
Lab #2: Introduction to Javascript
Javascript variables and datatypes
Introduction to Javascript
Javascript functions
Javascript 101
Introduction to react_js
3. Java Script
Angular - Chapter 7 - HTTP Services
Javascript
Javascript
ReactJS presentation
JavaScript Conditional Statements
Java Script ppt
JavaScript: Variables and Functions
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
Java 8 presentation
JavaScript Programming
Php introduction
Node.js Express
Lab #2: Introduction to Javascript
Ad

Similar to JavaScript - Chapter 4 - Types and Statements (20)

PPTX
Introduction to Client-Side Javascript
PDF
PDF
Javascript - Tutorial
PPT
13665449.ppt
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
PPTX
Paca java script slid
PPT
Javascript
PDF
JavaScript: Core Part
PPT
Javascript sivasoft
PPT
JavaScript - An Introduction
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
PDF
JavaScript for beginners
PPTX
Java script
PPTX
Cordova training : Day 3 - Introduction to Javascript
PPTX
Class[2][29th may] [javascript]
PDF
Client sidescripting javascript
PDF
javascript teach
PDF
JSBootcamp_White
PPTX
Unit - 4 all script are here Javascript.pptx
PPSX
DIWE - Programming with JavaScript
Introduction to Client-Side Javascript
Javascript - Tutorial
13665449.ppt
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
Paca java script slid
Javascript
JavaScript: Core Part
Javascript sivasoft
JavaScript - An Introduction
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
JavaScript for beginners
Java script
Cordova training : Day 3 - Introduction to Javascript
Class[2][29th may] [javascript]
Client sidescripting javascript
javascript teach
JSBootcamp_White
Unit - 4 all script are here Javascript.pptx
DIWE - Programming with JavaScript
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
PDF
Career Building in AI - Technologies, Trends and Opportunities
PDF
Webstack Academy - Internship Kick Off
PDF
Building Your Online Portfolio
PDF
Front-End Developer's Career Roadmap
PDF
Angular - Chapter 9 - Authentication and Authorization
PDF
Angular - Chapter 6 - Firebase Integration
PDF
Angular - Chapter 5 - Directives
PDF
Angular - Chapter 4 - Data and Event Handling
PDF
Angular - Chapter 3 - Components
PDF
Angular - Chapter 2 - TypeScript Programming
PDF
Angular - Chapter 1 - Introduction
PDF
JavaScript - Chapter 14 - Form Handling
PDF
JavaScript - Chapter 12 - Document Object Model
PDF
JavaScript - Chapter 11 - Events
PDF
JavaScript - Chapter 1 - Problem Solving
PDF
jQuery - Chapter 4 - DOM Handling
PDF
jQuery - Chapter 5 - Ajax
Webstack Academy - Course Demo Webinar and Placement Journey
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Course Demo Webinar - Full Stack Developer Course
Career Building in AI - Technologies, Trends and Opportunities
Webstack Academy - Internship Kick Off
Building Your Online Portfolio
Front-End Developer's Career Roadmap
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 5 - Directives
Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 3 - Components
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 1 - Introduction
JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 11 - Events
JavaScript - Chapter 1 - Problem Solving
jQuery - Chapter 4 - DOM Handling
jQuery - Chapter 5 - Ajax

Recently uploaded (20)

PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
Modernizing your data center with Dell and AMD
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PPTX
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Sensors and Actuators in IoT Systems using pdf
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
madgavkar20181017ppt McKinsey Presentation.pdf
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
NewMind AI Monthly Chronicles - July 2025
Chapter 3 Spatial Domain Image Processing.pdf
How to Build Crypto Derivative Exchanges from Scratch.pptx
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
Modernizing your data center with Dell and AMD
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Enable Enterprise-Ready Security on IBM i Systems.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx
Sensors and Actuators in IoT Systems using pdf
Belt and Road Supply Chain Finance Blockchain Solution
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...

JavaScript - Chapter 4 - Types and Statements

  • 4. www.webstackacademy.com ‹#› JS - Reserved Words ● Any language has a set of words called vocabulary ● JavaScript also has a set of keywords with special purpose ● These special purpose keywords words are used to construct statements as per language syntax and cannot be used as JavaScript variables, functions, methods or object names ● Therefore, also called reserve keywords ● The limited set of keywords help us to write unlimited statements
  • 5. www.webstackacademy.com ‹#› Reserved Words abstract debugger final instanceof protected throws boolean default finally int public transient break delete float interface return true byte do for let short try case double function long static typeof catch else goto native super var char enum if new switch void class export implements null synchronized volatile const extend import package this while continue false in private throw with *keywords in red color are removed from ECMA script 5/6
  • 7. www.webstackacademy.com ‹#› JS - Variables ● Variables are container to store values ● Name must start with – A letter (a to z or A to Z) – Underscore( _ ) – Or dollar( $ ) sign ● After first letter we can use digits (0 to 9) – Example: x1, y2, ball25, a2b
  • 8. www.webstackacademy.com ‹#› JS - Variables ● JavaScript variables are case sensitive, for example ‘sum’ and ‘Sum’ and ‘SUM’ are different variables Example : var x = 6; var y = 7; var z = x+y;
  • 9. www.webstackacademy.com ‹#› JS - Data Types ● There are two types of Data Types in JavaScript – Primitive data type – Non-primitive (reference) data type Note : JavaScript is weakly typed. Every JavaScript variable has a data type , that type can change dynamically
  • 10. www.webstackacademy.com ‹#› Primitive data type ● String ● Number ● Boolean ● Null ● Undefined
  • 11. www.webstackacademy.com ‹#› Primitive data type (String) ● String - A series of characters enclosed in quotation marks either single quotation marks ( ' ) or double quotation marks ( " ) Example : var name = “Webstack Academy”; var name = 'Webstack Academy';
  • 12. www.webstackacademy.com ‹#› Primitive data type (Number) ● All numbers are represented in IEEE 754-1985 double precision floating point format (64 bit) ● All integers can be represented in -253 to +253 range ● Largest floating point magnitude can be ± 1.7976x10308 ● Smallest floating point magnitude can be ± 2.2250x10-308 ● If number exceeds the floating point range, its value will be infinite
  • 13. www.webstackacademy.com ‹#› ● Floating point number is a formulaic representation which approximates a real number ● The most popular code for representing real numbers is called the IEEE Floating-Point Standard Sign Exponent Mantissa Float (32 bits) Single Precision 1 bit 8 bits 23 bits Double (64 bits) Double Precision 1 bit 11 bits 52 bits Primitive data type (Number) Float : V = (-1)s * 2(E-127) * 1.F Double : V = (-1)s * 2(E-1023) * 1.F
  • 14. www.webstackacademy.com ‹#› Primitive data type (Number formats) ● The integers can be represented in decimal, hexadecimal, octal or binary Example : var a = 0x10; // Hexadecimal var b = 010; // Octal number var c = 0b10; // Binary var num1 = 5; // Decimal var num2 = -4.56; // Floating point
  • 15. www.webstackacademy.com ‹#› Primitive data type (Number conversion) ● Converting from string Example : var num1 = 0, num2 = 0; // converting string to number num1 = Number("35"); num2 = Number.parseInt(“237”);
  • 16. www.webstackacademy.com ‹#› Primitive data type (Number conversion) ● Converting to string Example : var str = “”; // Empty string var num1 = 125; // converting number to string str = num1.toString();
  • 17. www.webstackacademy.com ‹#› Primitive data type (Number – special values) Special Value Cause Comparison Infinity, -Infinity Number too large or too small to represent All infinity values compare equal to each other NaN (not-a- number) Undefined operation NaN never compare equal to anything (even itself)
  • 18. www.webstackacademy.com ‹#› Primitive data type (Number – special value checking) Method Description isNaN(number) To test if number is NaN isFinite(number) To test if number is finite
  • 19. www.webstackacademy.com ‹#› Primitive data type (Boolean) ● Boolean data type is a logical true or false Example : var ans = true;
  • 20. www.webstackacademy.com ‹#› Primitive data type (Null) Example : var num = null; // value is null but still type is an object. ● In JavaScript the data type of null is an object ● The null means empty value or nothing
  • 21. www.webstackacademy.com ‹#› Primitive data type (Undefined) Example : var num; // undefined ● A variable without a value is undefined ● The type is object
  • 23. www.webstackacademy.com ‹#› Arrays ● Array represents group of similar values ● Array items are separated by commas ● Array can be declared as : – var fruits = [“Apple” , “Mango”, “Orange”];
  • 24. www.webstackacademy.com ‹#› Objects ● An Object is logically a collection of properties ● Objects represents instance through which we can access members ● Object properties are written as name:value pairs separated by comma Example : var student={ Name:“Mac”, City:“Banglore”, State:“Karnataka”};
  • 26. www.webstackacademy.com ‹#› JS – Simple Statements ● In JavaScript statements are instructions to be executed by web browser (JavaScript core) Example : <script> var y = 4, z = 7; // statement var x = y + z; // statement document.write("x = " + x); </script>
  • 27. www.webstackacademy.com ‹#› JS – Compound Statements Example : <script> ... if (num1 > num2) { if (num1 > num3) { document.write("Hello"); } else { document.write("World"); } } ... </script>
  • 28. www.webstackacademy.com ‹#› JS – Conditional Construct Conditional Constructs Iteration Selection for do while while if and its family switch case
  • 29. www.webstackacademy.com ‹#› JS – Statements (conditional - if) Syntax : if (condition) { statement(s); } Example : <script> var num = 2; if (num < 5) { document.write("num < 5"); } </script>
  • 30. www.webstackacademy.com ‹#› JS – Statements (conditional : if-else) Syntax : if (condition) { statement(s); } else { statement(s); }
  • 31. www.webstackacademy.com ‹#› JS – Statements (conditional : if-else) Example : <script> var num = 2; if (num < 5) { document.write("num is smaller than 5"); } else { document.write("num is greater than 5"); } </script>
  • 32. www.webstackacademy.com ‹#› JS – Statements (conditional : if-else if) Syntax : if (condition1) { statement(s); } else if (condition2) { statement(s); } else { statement(s); }
  • 33. www.webstackacademy.com ‹#› JS – Statements (conditional : if-else if) Example : <script> var num = 2; if (num < 5) { document.write("num is smaller than 5"); } else if (num > 5) { document.write("num is greater than 5"); } else { document.write("num is equal to 5"); } </script>
  • 34. www.webstackacademy.com ‹#› JavaScript - Input Method Description prompt() It will asks the visitor to input Some information and stores the information in a variable confirm() Displays dialog box with two buttons ok and cancel
  • 35. www.webstackacademy.com ‹#› Example - confirm() Example : <script> var x = confirm('Do you want to continue?'); if (x == true) { alert("You have clicked on Ok Button."); } else { alert("You have clicked on Cancel Button."); } </script>
  • 36. www.webstackacademy.com ‹#› Example - prompt() Example : <script> var person = prompt("Please enter your name", ""); if (person != null) { document.write("Hello " + person + "! How are you today?"); } </script>
  • 37. www.webstackacademy.com ‹#› Exercise ● Write a JavaScript program to input Name, Address, Phone Number and city using prompt() and display them ● Write a JavaScript program to find area and perimeter of rectangle ● Write a JavaScript program to find simple interest – Total Amount = principle * (1 + r * t)
  • 38. www.webstackacademy.com ‹#› Class Work ● WAP to find the max of two numbers ● WAP to print the grade for a given percentage ● WAP to find the greatest of given 3 numbers ● WAP to find the middle number (by value) of given 3 numbers
  • 39. www.webstackacademy.com ‹#› JS – Statements (switch) Syntax : switch (expression) { case exp1: statement(s); break; case exp2: statement(s); break; default: statement(s); } expr code true false case1? break case2? default code break code break true false
  • 40. www.webstackacademy.com ‹#› JS – Statements (switch) <script> var num = Number(prompt("Enter the number!", "")); switch(num) { case 10 : document.write("You have entered 10"); break; case 20 : document.write("You have entered 20"); break; default : document.write("Try again"); } </script>
  • 41. www.webstackacademy.com ‹#› Class work ● Write a simple calculator program – Ask user to enter two numbers – Ask user to enter operation (+, -, * or /) – Perform operation and print result
  • 42. www.webstackacademy.com ‹#› JS – Statements (while) Syntax: while (condition) { statement(s); } ● Controls the loop. ● Evaluated before each execution of loop body false true cond? code
  • 43. www.webstackacademy.com ‹#› JS – Statements (while) Example: <script> var iter = 0; while(iter < 5) { document.write("Looped " + iter + " times <br>"); iter = iter + 1; } </script>
  • 44. www.webstackacademy.com ‹#› JS – Statements (do - while) Syntax: do { statement(s); } while (condition); ● Controls the loop. ● Evaluated after each execution of loop body false true cond? code
  • 45. www.webstackacademy.com ‹#› JS – Statements (do-while) Example: <script> var iter = 0; do { document.write("Looped " + iter + " times <br>"); iter = iter + 1; } while ( iter < 5 ); </script>
  • 46. www.webstackacademy.com ‹#› JS – Statements (for loop) Syntax: for (init-exp; loop-condition; post-eval-exp) { statement(s); }; ● Controls the loop. ● Evaluated before each execution of loop body false true cond? code
  • 47. www.webstackacademy.com ‹#› JS – Statements (for loop) Execution path: for (init-exp; loop-condition; post-eval-exp) { statement(s); }; 1 2 3 4
  • 48. www.webstackacademy.com ‹#› JS – Statements (for loop) Example: <script> for (var iter = 0; iter < 5; iter = iter + 1) { document.write("Looped " + iter + " times <br>"); } </script>
  • 49. www.webstackacademy.com ‹#› JS – Statements (for-in loop) ● “for-in” loop is used to iterate over enumerable properties of an object Syntax: for (variable in object) { statement(s); }
  • 50. www.webstackacademy.com ‹#› JS – Statements (for-in loop) Example: <script> var person = { firstName:”Rajani”, lastName:”Kanth”, profession:”Actor” }; for (var x in person) { document.write(person[x] + “ “); } </script>
  • 51. www.webstackacademy.com ‹#› JS – Statements (for-in loop) ● loop only iterates over enumerable properties ● “for-in” loop iterates over the properties of an object in an arbitrary order ● “for-in” should not be used to iterate over an Array where the index order is important
  • 52. www.webstackacademy.com ‹#› JS – Statements (for-of loop) ● “for-of” loop is used to iterate over iterate-able object ● “for-of” loop is not part of ECMA script Syntax: for (variable of object) { statement(s); }
  • 53. www.webstackacademy.com ‹#› JS – Statements (for-of loop) Example: <script> var array = [1, 2, 3, 4, 5]; for (var x of array) { document.write(x + “ “); } </script>
  • 54. www.webstackacademy.com ‹#› Class Work ● W.A.P to print the power of two series using for loop – 21, 22, 23, 24, 25 ... ● W.A.P to print the power of N series using Loops – N1, N2, N3, N4, N5 ... ● W.A.P to multiply 2 numbers without multiplication operator ● W.A.P to check whether a number is palindrome or not
  • 55. www.webstackacademy.com ‹#› Class Work - Pattern ● Read total (n) number of pattern chars in a line (number should be “odd”) ● Read number (m) of pattern char to be printed in the middle of line (“odd” number) ● Print the line with two different pattern chars ● Example – Let's say two types of pattern chars '$' and '*' to be printed in a line. Total number of chars to be printed in a line are 9. Three '*' to be printed in middle of line. ● Output ==> $$$* * *$$$
  • 56. www.webstackacademy.com ‹#› Class Work - Pattern ● Based on previous example print following pyramid * * * * * * * * * * * * * * * *
  • 57. www.webstackacademy.com ‹#› Class Work - Pattern ● Based on previous example print following rhombus * * * * * * * * * * * * * * * * * * * * * * * * *
  • 58. www.webstackacademy.com ‹#› JS – Statements (break) ● A break statement shall appear only in “switch body” or “loop body” ● “break” is used to exit the loop, the statements appearing after break in the loop will be skipped ● “break” without label exits/‘jumps out of’ containing loop ● “break” with label reference jumps out of any block
  • 59. www.webstackacademy.com ‹#› JS – Statements (break) Syntax: while (condition) { conditional statement break; } false true loop cond? code block cond? false break? true
  • 60. www.webstackacademy.com ‹#› JS – Statements (break) <script> for (var iter = 0; iter < 10; iter = iter + 1) { if (iter == 5) { break; } document.write("<br>iter = " + iter); } </script>
  • 61. www.webstackacademy.com ‹#› JS – Statements (break with label) Syntax: outer_loop: for (condition) { inner_loop: for(condition) { conditional statement break outer_loop; // jump out of outer_loop } }
  • 62. www.webstackacademy.com ‹#› JS – Statements (continue) ● A continue statement causes a jump to the loop-continuation portion, that is, to the end of the loop body ● The execution of code appearing after the continue will be skipped ● Can be used in any type of multi iteration loop
  • 63. www.webstackacademy.com ‹#› JS – Statements (continue) Syntax: while (condition) { conditional statement continue; } false true loop cond? code block cond? false continue? true code block
  • 64. www.webstackacademy.com ‹#› JS – Statements (continue) <script> for (var iter = 0; iter < 10; iter = iter + 1) { if (iter == 5) { continue; } document.write("<br>iter = " + iter); } </script>
  • 65. www.webstackacademy.com ‹#› JS – Statements (continue with label) Syntax: outer_loop: for (condition) { inner_loop: for(condition) { conditional statement continue outer_loop; // continue from outer_loop } }
  • 66. www.webstackacademy.com ‹#› JS – Statements (goto) ● This keyword has been removed from ECMA script 5/6 ● “goto” keyword is is not recommended to use ● Use break with label if necessary
  • 67. www.webstackacademy.com ‹#› Web Stack Academy (P) Ltd #83, Farah Towers, 1st floor,MG Road, Bangalore – 560001 M: +91-80-4128 9576 T: +91-98862 69112 E: [email protected]