JavaScript Data Types
Data types in JavaScript define the types of values that can be stored in a variable and the actions that can be taken with those values. Value manipulation is how computer programs operate, and the types provide the types of values that can be represented. Values are segments of these bits that represent information, and each value has a type that dictates its function. In the end, all data within a computer is stored as sequences of bits (zeroes and ones).
Primitive data types are essential components for expressing basic values in JavaScript. They are regarded as basic data structures with a type and a single value. Primitive types’ immutability is one of their main features. This implies that a primitive value cannot be altered once it has been created. Concatenating strings is an example of an operation that creates a new primitive value even though it appears to change a primitive.
Because JavaScript is a flexibly typed language, variables’ data types are determined by the value that is assigned to them rather than by the user expressly declaring the type. Values of all kinds can be stored in variables, which are names for values that are kept in memory.
Data Type Analysis
JavaScript offers the typeof operator to identify the kind of data a variable contains at any given time. A string specifying the data type of a variable or value is returned when typeof is applied to it. This built-in technique allows you to solve typical primitive-related difficulties without having to build your own logic.
JavaScript has seven primitive types: null, undefined, Boolean, BigInt, String, Number, and Symbol. Primitive values are transmitted by value, which means that their contents are copied when they are assigned to variables or provided to functions.
Let’s investigate a few of these archaic kinds:

Numbers: The numbers Numerical values are represented via JavaScript’s Number type. Numbers are kept as floating-point, 64-bit numbers. JavaScript, in contrast to many other programming languages, uses a single Number type to represent both integers (whole numbers) and floating points (numbers with decimal places). Literals are a direct way to write numbers in code.
let age = 26; // Number literal without decimal
let price = 99.99; // Number literal with decimal
let bigNumber = 1.7976931348623157e+308; // Example of a large number
let myNum = 1000; // Example variable assignment
console.log(typeof age); // Output: “number”
Number also includes special values like Infinity (indicating a value too huge to be expressed) and NaN (which means “not a number” and is the result of improper numeric operations like 0 divided by 0 or trying to convert non-numeric text to a number). Crucially, NaN is still regarded as a Number type value.
Strings: Strings Text is represented and stored using the String type. In essence, a string is a collection of characters. Characters are enclosed in quotations to form strings. Backticks (`…`), double quotes (“…”), and single quotes (‘…’) are all acceptable. A single character in JavaScript is represented as a string of length one; there is no distinct character type for JavaScript. Similar to other primitives, strings cannot be changed. Concatenation and other operations that seem to alter a string really create a new string.
let greeting = “Hello”; // String literal using double quotes
let name = ‘Alice’; // String literal using single quotes
let message = Hello
; // Template literal using backticks, allows embedding variables/expressions
console.log(greeting); // Output: “Hello”
console.log(message); // Output: “Hello, Alice!”
console.log(typeof greeting); // Output: “string”
Booleans: Booleans Logical values that are either true or false are represented by the Boolean type. This kind is helpful for assessing circumstances and making decisions in programs. True and false are the only two possible boolean values. These are written in literal form. Boolean values are frequently returned by comparison operators.
let isLearning = true; // Boolean literal true
let isFinished = false; // Boolean literal false
console.log(isLearning); // Output: true
console.log(typeof isFinished); // Output: “boolean”
// Boolean values are often the result of comparisons or logical operations
let num1 = 5;
let num2 = 10;
console.log(num1 > 0 && num2 > 0); // Logical AND operator result
console.log(num1 == num2); // Equality comparison result
The notion of truthy and falsy values is present in JavaScript. Values of other kinds are transformed into booleans when they are used in an if statement or other situation where a boolean is expected. While truthy values become true, false values become false. Undefined, null, false, 0, NaN, and the empty string (”) are examples of false values. Every other value is accurate.
Undefined: Not specified A unique primitive value known as the undefined type indicates that a variable has been declared but has not yet been given a value. When a variable isn’t explicitly assigned a value, this is its default starting value. Another option is to explicitly designate a variable as undefined.
let unassignedVariable; // Declared but no value assigned, implicitly undefined
let val1 = undefined; // Explicitly assigned undefined
console.log(unassignedVariable); // Output: undefined
console.log(val1); // Output: undefined
console.log(typeof unassignedVariable); // Output: “undefined”
Null: Nothing Another unique primitive value in JavaScript is the null type. It stands for the express declaration that a variable points to “no value” or the deliberate lack of any object value. Null is the absence of a value, which sets it apart from 0 or an empty string.
let emptyValue = null; // Explicitly assigned null
console.log(emptyValue); // Output: null
console.log(typeof emptyValue); // Output: “object” – This is a well-known quirk/error in JavaScript
Since null is categorised as a primitive type and does not have a constructor, some people view the fact that typeof null returns “object” as a defect. Null is also a false value, much like undefined. Null and undefined are equal to one other but not to any other value when using ==.
Understanding these foundational data types is essential because JavaScript builds objects and arrays on them.
Data Type Conversion and Modification
Primitive values are immutable, although you can convert them across data types. JavaScript automatically converts types, or coerces, when an operation expects a different type. For instance, JavaScript will convert if you use a non-boolean value when a boolean is intended. Type conversions are also carried out by the abstract equality operator (==).
Nevertheless, you can also use the built-in JavaScript functions to explicitly convert types. Number(), String(), and Boolean() are particularly mentioned in the methods for converting values to the corresponding primitive kinds.
Explicit type conversions include the following examples:
// Converting to Number
let stringNumber = “123”;
let convertedNumber = Number(stringNumber);
console.log(convertedNumber); // Output: 123
console.log(typeof convertedNumber); // Output: “number”
let booleanTrue = true;
let convertedNumberFromBoolean = Number(booleanTrue);
console.log(convertedNumberFromBoolean); // Output: 1 (true converts to 1)
// Converting to String
let numericValue = 456;
let convertedString = String(numericValue);
console.log(convertedString); // Output: “456”
console.log(typeof convertedString); // Output: “string”
let booleanFalse = false;
let convertedStringFromBoolean = String(booleanFalse);
console.log(convertedStringFromBoolean); // Output: “false”
// Converting to Boolean
// JavaScript has “falsy” values: false, 0, “”, null, undefined, NaN
// All other values are “truthy”
let zeroValue = 0;
let emptyString = “”;
let nullValue = null;
let undefinedValue = undefined;
let nonZeroNumber = 10;
let nonEmptyString = “abc”;
console.log(Boolean(zeroValue)); // Output: false
console.log(Boolean(emptyString)); // Output: false
console.log(Boolean(nullValue)); // Output: false
console.log(Boolean(undefinedValue)); // Output: false
console.log(Boolean(nonZeroNumber)); // Output: true
console.log(Boolean(nonEmptyString)); // Output: true
console.log(typeof Boolean(true)); // Output: “boolean”
In conclusion, JavaScript programmers must learn typeof and how to convert data types manually or implicitly to work with the many values they control.