Page 186 - Web_Application_v2.0_C12_Fb
P. 186
2.4 JAVASCRIPT DATA TYPES
In JavaScript, a data type defines the kind of value a variable can hold. data types are categorized into two
main types: primitive and non-primitive (or reference) data types.
Data Type
Primitive Datatypes Non-Primitive Datatypes
Strings Numbers: Integers Boolean Undefined Null Objects Array Functions
Floating-Point,
Exponential, NaN,
Infinity
Primitive Data Types
Primitive data types are the basic building blocks of data in JavaScript. They are immutable (cannot be
changed) and are stored directly in the variable. The primitive data types in JavaScript are:
String: Represents a sequence of characters enclosed in single quotes, double quotes, or backticks.
For example:
var sentence1 = "Hello, this is sentence one! "; //primitive type or string literal
var sentence2 = 'This is a string sentence two.';
var sentence3 = 'She said, "This is sentence three inside quotes."';
Number: A number data type can be represented in various forms such as integers, floating-point values,
exponential values, NaN, and Infinity. Here's how they are used:
Integer Value: An integer value is a whole number without any fractional or decimal part. It can be
positive, negative, or zero.
For example:
var a = 250; // an integer
Floating-Point Value: A floating-point value is a number that includes a decimal point, allowing for
representation of real numbers with fractional parts.
For example:
var b = 25.5; // a number with a decimal
Exponential Value: An exponential value represents a number in the form of a base and an exponent,
written as base e^exponent.
For example:
var c = 20e5; // an exponential value, which evaluates to 20 * 200000
NaN (Not a Number): This occurs when an operation involves non-numeric values.
'hi' * 3; // results in NaN
Infinity: JavaScript includes special numeric values. When a number is divided by 0, the result is Infinity.
For example:
var d = 3/0 // results in Infinity
184 Touchpad Web Applications (Ver. 2.0)-XII

