Page 76 - DCAP202_Fundamentals of Web Programming
P. 76
Unit 6: Introduction to JavaScript
6.6.1 Primitive Data Types Notes
Primitive data types are the simplest building blocks of a program. They are types that can be
assigned a single literal value such as the number 5.7, or a string of characters such as “hello”.
JavaScript supports three core or basic data types:
numeric
string
Boolean
In addition to the three core data types, there are two other special types that consist of a single
value:
null
undefined
Numeric Literals: JavaScript supports both integers and floating-point numbers. Integers
are whole numbers and do not contain a decimal point; e.g., 123 and –6. Integers can be
expressed in decimal (base 10), octal (base 8), and hexadecimal (base 16), and are either
positive or negative values.
Floating-point numbers are fractional numbers such as 123.56 or –2.5. They must contain
a decimal point or an exponent specifier, such as 1.3e–2. The letter “e” for exponent notation
can be either uppercase or lowercase.
JavaScript numbers can be very large (e.g., 10 or 10 –308 ).
308
Table 6.1: Numeric Literals
12345 Integer
23.45 Float
.234E–2 scientific notation
.234e+3 scientific notation
0x456fff Hexadecimal
0x456FFF Hexadecimal
0777 Octal
String Literals and Quoting: String literals are rows of characters enclosed in either double
or single quotes. The quotes must be matched. If the string starts with a single quote, it
must end with a matching single quote, and likewise if it starts with a double quote, it
must end with a double quote. Single quotes can hide double quotes, and double quotes
can hide single quotes:
“This is a string”
‘This is another string’
“This is also ‘a string’ “
‘This is “a string”’
An empty set of quotes is called the null string. If a number is enclosed in quotes, it is considered
a string; e.g., “5” is a string, whereas 5 is a number.
LOVELY PROFESSIONAL UNIVERSITY 69