2. JavaScript DataTypes
JavaScript allows you to work with three primitive data types:
• Numbers eg. 123, 120.50 etc.
• Strings of text e.g. "This text string" etc.
• Boolean e.g. true or false.
JavaScript also defines two trivial data types, null and undefined,
each of which defines only a single value.
In addition to these primitive data types, JavaScript supports a
composite data type known as object. We will see an object
detail in a separate chapter.
JavaScript Variables:
 Like many other programming languages, JavaScript has
variables. Variables can be thought of as named containers. You
can place data into these containers and then refer to the data
simply by naming the container.
• Before you use a variable in a JavaScript program, you must
declare it. Variables are declared with the var keyword as follows:
<script type="text/javascript">
<!–
var money;
var name;
//-->
</script>
You can also declare multiple variables with the same var keyword
as follows:
<script type="text/javascript">
<!–
var money, name;
//-->
</script>
Note: Use the var keyword only for declaration or
initialization.once for the life of any variable name in a
document. You should not re-declare same variable twice.
Client-side JavaScript:
JavaScript Variable Scope
• The scope of a variable is the region of your program in which it
is defined. JavaScript variable will have only two scopes.
– Global Variables: A global variable has global scope which means it is
defined everywhere in your JavaScript code.
– Local Variables: A local variable will be visible only within a function where it
is defined. Function parameters are always local to that function.
<script type="text/javascript">
<!–
var myVar = "global"; // Declare a global variable
function checkscope( )
{ var myVar = "local"; // Declare a local variable
document.write(myVar);
}
//-->
</script>
JavaScript Variable Names:
• You should not use any of the JavaScript reserved keyword as
variable name. These keywords are mentioned in the next
section. For example, break or boolean variable names are not
valid.
• JavaScript variable names should not start with a numeral (0-9).
They must begin with a letter or the underscore character. For
example, 123test is an invalid variable name but _123test is a valid
one.
• JavaScript variable names are case sensitive. For example, Name
and name are two different variables.
JavaScript Reserved Words:
3. JavaScript Operators
 Arithmetic Operators
 Comparision Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators
Assume variable A holds 10 and variable B holds 20 then:
The Arithmatic Operators:
The Comparison Operators:
The Logical Operators:
The Bitwise Operators:
The Assignment Operators:
• Note: Same logic applies to Bitwise operators so they will become like <<=, >>=,
>>=, &=, |= and ^=.
Miscellaneous Operator
The Conditional Operator (? :)
The typeof Operator
• The typeof is a unary operator that is placed before its single
operand, which can be of any type. Its value is a string indicating
the data type of the operand.
• The typeof operator evaluates to "number", "string", or "boolean"
if its operand is a number, string, or boolean value and returns
true or false based on the evaluation.
4. Conditional statements:
In JavaScript we have the following conditional statements:
• if statement - use this statement to execute some code only if
a specified condition is true
• if...else statement - use this statement to execute some code if
the condition is true and another code if the condition is false
• if...else if....else statement - use this statement to select one
of many blocks of code to be executed
switch statement - use this statement to select one of many
blocks of code to be executed
Example 1:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>function myFunction()
{
var x="";
var time=new Date().getHours();
if (time<10) { x="Good morning"; }
else if (time<20) { x="Good day"; }
else { x="Good evening"; }
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
Example 2: switch statement
Demonstrate with FreeJavaScript Editor

More Related Content

PPT
Javascript
PPTX
Js datatypes
PPT
data-types-operators-datatypes-operators.ppt
PPTX
Basics of Javascript
PPTX
Java script basics
PDF
PPTX
javascript client side scripting la.pptx
PPTX
Java script
Javascript
Js datatypes
data-types-operators-datatypes-operators.ppt
Basics of Javascript
Java script basics
javascript client side scripting la.pptx
Java script

Similar to JS-Slides-2_74526582652945_73562876535.ppt (20)

PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
PDF
Javascript tutorial basic for starter
PPTX
Java script
DOCX
PPTX
Java script.pptx v
PDF
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
PPSX
DIWE - Programming with JavaScript
PPTX
JavaScript.pptx
PPT
An introduction to javascript
PPTX
Javascript
PPT
Java Script
PDF
Java Script
PPT
JavaScript ppt for introduction of javascripta
PPT
Presentation JavaScript Introduction Data Types Variables Control Structure
PDF
Javascript - Tutorial
PPTX
JavaScript Fundamentals & JQuery
PPT
JavaScript - An Introduction
PPTX
WT Unit-3 PPT.pptx
PPTX
Javascript analysis
PDF
any things about myself and that css tble
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
Javascript tutorial basic for starter
Java script
Java script.pptx v
javascript-variablesanddatatypes-130218094831-phpapp01.pdf
DIWE - Programming with JavaScript
JavaScript.pptx
An introduction to javascript
Javascript
Java Script
Java Script
JavaScript ppt for introduction of javascripta
Presentation JavaScript Introduction Data Types Variables Control Structure
Javascript - Tutorial
JavaScript Fundamentals & JQuery
JavaScript - An Introduction
WT Unit-3 PPT.pptx
Javascript analysis
any things about myself and that css tble
Ad

Recently uploaded (20)

PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
The various Industrial Revolutions .pptx
PDF
Flame analysis and combustion estimation using large language and vision assi...
PDF
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Zenith AI: Advanced Artificial Intelligence
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPT
What is a Computer? Input Devices /output devices
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Convolutional neural network based encoder-decoder for efficient real-time ob...
PPTX
Configure Apache Mutual Authentication
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPT
Geologic Time for studying geology for geologist
PPT
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
UiPath Agentic Automation session 1: RPA to Agents
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
Enhancing emotion recognition model for a student engagement use case through...
The various Industrial Revolutions .pptx
Flame analysis and combustion estimation using large language and vision assi...
Two-dimensional Klein-Gordon and Sine-Gordon numerical solutions based on dee...
Getting started with AI Agents and Multi-Agent Systems
Zenith AI: Advanced Artificial Intelligence
Taming the Chaos: How to Turn Unstructured Data into Decisions
What is a Computer? Input Devices /output devices
Custom Battery Pack Design Considerations for Performance and Safety
A comparative study of natural language inference in Swahili using monolingua...
Convolutional neural network based encoder-decoder for efficient real-time ob...
Configure Apache Mutual Authentication
NewMind AI Weekly Chronicles – August ’25 Week III
Geologic Time for studying geology for geologist
Galois Field Theory of Risk: A Perspective, Protocol, and Mathematical Backgr...
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
sbt 2.0: go big (Scala Days 2025 edition)
UiPath Agentic Automation session 1: RPA to Agents
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Ad

JS-Slides-2_74526582652945_73562876535.ppt

  • 1. 2. JavaScript DataTypes JavaScript allows you to work with three primitive data types: • Numbers eg. 123, 120.50 etc. • Strings of text e.g. "This text string" etc. • Boolean e.g. true or false. JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value. In addition to these primitive data types, JavaScript supports a composite data type known as object. We will see an object detail in a separate chapter.
  • 2. JavaScript Variables:  Like many other programming languages, JavaScript has variables. Variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. • Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows: <script type="text/javascript"> <!– var money; var name; //--> </script>
  • 3. You can also declare multiple variables with the same var keyword as follows: <script type="text/javascript"> <!– var money, name; //--> </script> Note: Use the var keyword only for declaration or initialization.once for the life of any variable name in a document. You should not re-declare same variable twice. Client-side JavaScript:
  • 4. JavaScript Variable Scope • The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes. – Global Variables: A global variable has global scope which means it is defined everywhere in your JavaScript code. – Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function. <script type="text/javascript"> <!– var myVar = "global"; // Declare a global variable function checkscope( ) { var myVar = "local"; // Declare a local variable document.write(myVar); } //--> </script>
  • 5. JavaScript Variable Names: • You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid. • JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid one. • JavaScript variable names are case sensitive. For example, Name and name are two different variables.
  • 7. 3. JavaScript Operators  Arithmetic Operators  Comparision Operators  Logical (or Relational) Operators  Assignment Operators  Conditional (or ternary) Operators
  • 8. Assume variable A holds 10 and variable B holds 20 then: The Arithmatic Operators:
  • 12. The Assignment Operators: • Note: Same logic applies to Bitwise operators so they will become like <<=, >>=, >>=, &=, |= and ^=.
  • 13. Miscellaneous Operator The Conditional Operator (? :) The typeof Operator • The typeof is a unary operator that is placed before its single operand, which can be of any type. Its value is a string indicating the data type of the operand. • The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value and returns true or false based on the evaluation.
  • 14. 4. Conditional statements: In JavaScript we have the following conditional statements: • if statement - use this statement to execute some code only if a specified condition is true • if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false • if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be executed
  • 15. Example 1: <!DOCTYPE html> <html> <body> <p>Click the button to get a time-based greeting.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x=""; var time=new Date().getHours(); if (time<10) { x="Good morning"; } else if (time<20) { x="Good day"; } else { x="Good evening"; } document.getElementById("demo").innerHTML=x; } </script> </body> </html>
  • 16. Example 2: switch statement Demonstrate with FreeJavaScript Editor