MakeW3web

JavaScript Data types

JavaScript is a dynamically typed language, meaning that variables can hold values of different data types, and the type of the variable can change during the execution of a program. However, JavaScript has a number of built-in data types that you'll commonly encounter when working with variables and values. Here's an overview of the most important JavaScript data types:

  1. Number:

The `Number` data type represents both integers and floating-point numbers (decimals). JavaScript uses the IEEE 754 standard for floating-point arithmetic, which means that it represents all numbers as 64-bit floating-point numbers. Examples of `Number` values:

var intVal = 42;
var floatVal = 3.14;
  1. String:

The `String` data type represents sequences of characters. Strings in JavaScript can be created using single quotes (`'`), double quotes (`"`), or backticks (```), also known as template literals. Examples of `String` values:

var singleQuotes = 'Hello, World!';
var doubleQuotes = "Hello, World!";
var templateLiteral = `Hello, World!`;
  1. Boolean:

The `Boolean` data type represents true or false values. It's commonly used in conditional statements, comparisons, and to represent the state of a program. Examples of `Boolean` values:

var isTrue = true;
var isFalse = false;
  1. Null:

The `null` data type represents an intentionally absent value or "no value". It's used to indicate that a variable should have no value or reference.

var emptyVar = null;
  1. Undefined:

A variable that has been declared but not assigned a value is of type `undefined`. It represents the absence of a value, similar to `null`, but it indicates that a variable has not been initialized.

var notDefined;
console.log(notDefined); // Outputs: undefined
  1. Object:

The `Object` data type represents a collection of properties, where each property is a key-value pair. Objects in JavaScript can be created using object literals, which are defined using curly braces (`{}`), or using the new `Object()` constructor. Examples of `Object` values:

var objLiteral = { key: 'value' };
var objConstructor = new Object();
  1. Array:

The `Array` data type represents an ordered list of values, where each value can be of any data type. Arrays are a specialized type of `Object`. Arrays in JavaScript can be created using array literals, which are defined using square brackets (`[]`), or using the `new Array()` constructor. Examples of `Array` values:

var arrLiteral = [1, 2, 3];
var arrConstructor = new Array(1, 2, 3);

In addition to these data types, JavaScript also has support for `Symbol` and `BigInt`:

  • `Symbol`: Represents unique, immutable identifiers that can be used as object property keys.
  • `BigInt`: Represents integers of arbitrary length, allowing for the representation of very large integer values beyond the safe integer limit of `Number`.

In summary, JavaScript has several built-in data types, such as `Number`, `String`, `Boolean`, `Null`, `Undefined`, `Object`, and `Array`. As a dynamically typed language, JavaScript allows variables to hold values of different data types, and the type of a variable can change during the execution of a program. Understanding these data types is essential for working effectively with JavaScript.

Was this page helpful?