Chapter 2
Data Types
Primitive Data Types
• Character (blood=“A”)
• Number (num1=100, price=35.5)
• Boolean
• String (name= “aye aye”)
Primitive Data Types for JS
• Number
• Boolean
• String
JS does not have Character data type.
Number does not have integer and float. It has 64-bit float.
JS is Dynamic type language.
Type Juggling
1
Type coercion
"ABC" > 123
No Error. Convert ABC to number. The value is zero. Zero is not greater than 123. So , the result
is false.
String
Single Quote, Double Quote and Back tick
let blood = "A" (using double quote)
let greet = 'Hello' (using single quote)
let time = "3 O' Clock" (no error)
let time = '3 O' Clock' (error)
let time = '3 O\' Clock' (no error with using Backslash)
let name = "Bob"
let greet = `Hello Alice` (using back tick)
Template String using back tick
let name = "Bob"
let greet = `Hello ${name}`
let name = "Bob"
let age = 22
let greet = `
Hello ${name},
you are ${age} years old.
2
Special Data Types
• Undefined
• null
• NaN
Undefined Variable
let myvar
null Variable
let myvar = null
NaN (Not a Number)
let num1
let num2 = 1
num1 + num2
num1→undefined
num2→number
num1+num2→NaN (undefined is not converted to number)
3
Exercises
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Numbers can be written with, or without decimals:</p>
<p id="demo"></p>
<script>
let x1 = 34.00;
let x2 = 34;
let x3 = 3.14;
[Link](x1 + "<br>" + x2 + "<br>" + x3)
</script>
</body>
</html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>You can use quotes inside a string, as long as they don't match the quotes
surrounding the string:</p>
<p id="demo"></p>
<script>
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';
[Link]( answer1 + "<br>" + answer2 + "<br>" + answer3)
</script>
</body>
</html>