Data Types - Exercise 1
Data types describe what kind of value JavaScript is working with, such as a number, string, or object. They affect which operations make sense and how JavaScript interprets the value.
const count = 3; // number
const title = "Guide"; // string
const active = true; // boolean
JavaScript has seven primitive data types: Number, String, Boolean, null, undefined, BigInt, and Symbol. Primitive values are not objects, and each represents one basic kind of data.
- Number
- String
- Boolean
- Null
- Undefined
- BigInt
- Symbol (added in ECMAScript 6)
You declare a number variable the same way as any other
variable. JavaScript uses one number type
for integers and floating-point values.
let num = 10;
let anotherNum = 20;
const PI = 3.14;
null represents the intentional absence
of a value, while undefined usually means
a value has not been assigned or is missing. For
example, an application might set user = null
after clearing a selected user.
You can create a string with single quotes, double quotes, or backticks. Backticks are especially useful when you need interpolation or multi-line text.
const str1 = 'Hello';
const str2 = "World";
let str3 = `JavaScript`;
The boolean data type represents one of
two logical values: true or
false. Booleans are commonly used as the
conditions of if statements and loops.
Declare a boolean by assigning either
true or false to a variable.
const isLoggedIn = true;
const hasPermission = false;
undefined means JavaScript does not currently have a value for something. A declared but uninitialized variable is one common example, while an object property can also be undefined when it is absent.
Null is an explicit value that usually means "no value" or "empty on purpose."
The typeof operator returns a string
describing the type of a value, such as
"number", "string", or
"boolean".