JavaScript Data Types – Strings, Numbers, Booleans, and Objects
JavaScript can work with many different types of data. Understanding data types is important because they determine how the program stores values and performs operations.
The most common primitive data types are:
String – textual values.Number – numeric values, including integers and decimal numbers.Boolean – logical values true and false.Undefined – a variable that has been declared but not assigned a value.Null – an intentional absence of a value.BigInt – integer values that are too large for the standard Number type.Symbol – unique values commonly used as property identifiers.
JavaScript also supports more complex data structures such as objects and arrays.
String – Text Data
JavaScript
const name = "Aleksandar";
const city = "Belgrade";
console.log("Hello, " + name + "!");
To combine text and variables more conveniently, JavaScript provides template literals, which use backticks:
JavaScript
const name = "Aleksandar";
const age = 35;
const message = `Hello, ${name}! You are ${age} years old.`;
console.log(message);
This approach is especially useful when multiple variables need to be included within a string.
Number – Numeric Values
JavaScript
const price = 2500;
const quantity = 3;
const total = price * quantity;
console.log(total);
The result is 7500 because JavaScript multiplies the price by the quantity.
Boolean – Logical Values
JavaScript
const isUserLoggedIn = true;
if (isUserLoggedIn) {
console.log("Welcome to your account!");
}
Boolean values are used to evaluate conditions and control program flow.
Undefined and Null
JavaScript
let result;
console.log(result); // undefined
const activeUser = null;
console.log(activeUser); // null
undefined typically indicates that a value has not yet been defined, while null is used when we intentionally want to represent the absence of a value.
Objects
Objects are used to store related data as key-value pairs.
JavaScript
const user = {
name: "Aleksandar",
age: 35,
city: "Belgrade"
};
console.log(user.name);
console.log(user.age);
Objects are one of the most important data structures in JavaScript and are widely used for representing users, products, settings, and application data.
Arrays
Arrays are used to store multiple values in a single variable.
JavaScript
const technologies = ["HTML", "CSS", "JavaScript"];
console.log(technologies[0]);
console.log(technologies[2]);
Output:
Plain Text
HTML
JavaScript
Arrays are commonly used for lists of products, articles, users, categories, and many other collections of data.
Checking Data Types
You can determine the type of a value using the typeof operator:
JavaScript
console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof {}); // object
Understanding data types is essential because variables, conditions, functions, objects, arrays, and nearly every other part of JavaScript rely on them. Choosing the appropriate data type makes code easier to understand, maintain, and debug.