Data Types
- In computer programming, a data type is a collection of data values. A data type or type for short is a convenient umbrella name for grouping data of similar values.
JavaScript has 8 data types namely String, Number, BigInt, Boolean, Undefined, Null, Symbol and Object. The first 7 data types are known as primitive data types and are not objects and so have no methods.
This is a series so you can check out my other articles on JavaScript to catch up. Highlighted texted are further explained in the footnote.
String
In JavaScript, strings are sequences of characters enclosed in single (' ') or double (" ") quotes. See the code below for some examples;
let Drink = 'Beer';
let String = "12@#String%";
Number
A number is a data type that represents numerical values. Numbers can be integers or decimal numbers(also known as floating-point numbers). JavaScript uses the Number primitive type to represent numbers.
Examples of numbers in JavaScript;
5 this is an integer
3.142 this is a decimal number or floating-point number
-10 this is a negative number
Numbers in JavaScript can be used in mathematical operations, comparisons, and other calculations.
BigInt
The BigInt type is a numeric primitive type that can represent integers with arbitrary magnitude. With BigInt, you can safely use a variable to store and operate on integers that are too big to be stored as a Javascript Number type. JavaScript integers are precise up to 15 digits but lose accuracy beyond this point.
You can create a BigInt number by using BigInt() or appending n to the end of an integer.
const randomNumber = 123456453627465;
const bigNumber = BigInt(7846637893774667577380123456723342);
const bigNumberToo = 4775867767n;
Boolean
A boolean value is one that can either be true or false. A boolean type gives a yes or no answer regarding something. Sometimes when programming you would need a type that gives two answers regarding a scenario and this is where a boolean data type comes in handy.
The Boolean() function is used to check if an expression is true or false. Copy, paste and run the code below on https://onecompiler.com/nodejs to see the output. It should read true, false.
let x = Boolean(10>2);
console.log(x)
let y = Boolean(4<1);
console.log(y)
Undefined
A variable in JavaScript that is without any value has a value of undefined. This means that a value has yet to be assigned to the variable after it's been declared so it is undefined. The data type undefined can also be assigned to a variable but it is preferred to use null data type instead to indicate an absence of value.
let building_type;
console.log(building_type)
let building_type2 = undefined;
Null
The null data type is used to indicate that something doesn't exist, for example a variable without a value. In JavaScript null is both a primitive and an object.
let carSize = null;
Symbol
Symbols are a new primitive data type introduced in the 2015 version of JavaScript. They are used to show unique values which cannot be changed. Symbols can be used as keys in objects. They are created with the function Symbol().
const g = Symbol('id'); // 1
const p = Symbol('id2'); // 2
let car = {
brand: 'Mercedes',
[id2]: 123
};
//1 --> A string with name 'id' can be passed as the description of the Symbol.
//2 --> The description of the Symbol which is 'id2' in this case is used as a key of the object car with value 123
Object
An object in programming is a collection of related properties. These properties are made up of key and value pairs. The code below illustrates one way of declaring an object.
const objectName = {
key1: value1,
key2: value2
}
const Car = {
color: "red",
brand: "Hyundai"
{
const human = {
height: 198,
weight: 120
{
A data value is the content that fills a space in a record(in a container). For example, in the code below the value of the variable car_colour is 'red' where 'red' is a string.
const car_colour = 'red';
Numerical refers to something expressed as a number or numbers
Primitive type - Primitive data types are a set of basic data types from which all other data types are constructed. Also we can only store one value of a particular primitive type in it's variable as opposed to an object.
let africanName = 'Kojo' ;
/*variable africanName can only store one
value with data type string because string is primitive data type */
let fictionBookAuthor = {
name: 'A.L Kennedy',
height: 176,
writer: true
}
/* The object fictionBookAuthor can hold various data types like
String, Number and Boolean */
Arbitrary is anything chosen on personal whim instead of having a good reason (random).
A function is a set of statements that performs a task in a procedural manner while taking in an input and returning an output.
Declaring a variable means creating a named placeholder to be used by a data type at a later date.
*/