JavaScript has eight different data types which determine what data can be stored and manipulated within a program.
Within those data types they can be split into three categories:
- Primary
- Reference
- Special
Primary data types are String, Number, and Boolean.
Reference data types are Objects, Arrays, and Functions (which are all types of objects)
Special data types are Undefined and Null.
As JavaScript is 'dynamically typed', variable values can switch data types as you please but can only hold one value at a time.
String
The String data type holds a single set or sequence of characters. In simple terms, it's used to hold text.
Strings can be created or assigned by using one of three types of quotes.
Single
1let single = 'String example';
Double
1let double = "String example";
Backtick
1let backtick = `String example`;
Both the single and double quote variations work exactly the same way.
Backticks allow us to implement variables and expressions which are called template literals.
1const name = 'Java5cript.com'2let template = `This website is called ${name}`3// This website is called Java5cript.com
Number
The Number data type holds both integer and floating-point numbers. In other words it can hold positive or negative numbers with or without decimal places, or numbers written using exponential notation.
For example:
1var one = 1; // integer2var twoPointOne = 2.1; // floating-point number3var expo = 2.25e+6; // exponential notation, same as 2.25e6 or 22500004var minusNum = -3 // minus integer
Infinity & Nan
Apart from the regular number types, the Number data type also includes some, 'special values' which are: Infinity, -Infinity and NaN.
Infinity represents the mathematical Infinity ∞, which is bigger than any number.
We can achieve infinity by dividing any nonzero number by 0, as shown below:
1console.log(20 / 0); // Infinity2console.log(-20 / 0); // Infinity
NaN which stands for 'Not A Number' represents the result of an incorrect or an undefined mathematical operation. For example, mixing Strings with a Number and an operation:
1console.log("a string" / 4); // Output: NaN
Number Operations
JavaScript allows you to use many different and helpful Number operations. Such as:
- Addition +
- Subtraction -
- Division /
- Multiplication *
- Exponentiation **
- Increment ++
- Decrement —
Boolean
The Boolean data type holds one of two values.
'true' or 'false'.
Also referred to as the 'logical type', Boolean types are often used for storing 'true' or 'false' or 'yes and 'no' values. For example:
1let loading = false // Item isn't loading2let loading = true // Item is loading
Boolean data types are also often used as the result of a comparison. For example, comparing two numbers making for much more readable code:
1const a = 10;2const b = 20;3let c = a > b;45console.log(c) // false
Undefined
The Undefined data type holds one special value, 'undefined'.
A variable is undefined if it has been declared but not assigned a value.
1let name = "Java5cript";2let domain;34console.log(name) // Java5cript5console.log(domain) // Undefined
Null
The Null data type holds one special value, 'null'.
A null value simply means nothing is being held. It isn't the same as an empty string (" "), nor is it equal to 0. You could even consider its value as “empty” or “unknown”.
You can however explicitly define a variable as null but it's important to remember a null value is considered falsy.
1let name = null;
Arrays
The Array data type holds multiple values of any data type in a single variable.
Defined as elements, each value in the array has a numeric position called the index.
JavaScript uses zero-based numbering so the index count starts at 0.
Arrays are created by using brackets [ ] around comma-separated elements:
1const myArray = ["Java5cript", "hello", 2, true];
The elements inside can be accessed using bracket notation:
1const myArray = ["Java5cript", "hello", 2, true];23myArray[0] // "Java5cript"4myArray[3] // true
Arrays are a broad subject which you can learn more about in an article coming soon.
Objects
The Object data type allows you to store collections of data.
Collections are referred to as properties which are held using key-value pairs.
On the left side is a property key which is always a string. On the right, is a value which can be any data type. As shown below, each property is separated by a comma.
1var obj = {} // Creates an empty object23var website = {4 "name" : 'Java5cript',5 "pages" : 55,6 "domain" : '.com'7}
Objects are a broad subject which you can learn more about in an article coming soon.
Functions
The Function data type holds a block of code that can be executed.
In JavaScript, functions are also objects so they can be assigned to variables creating a function expression.
Function can also be named functions or anonymous.
Functions are a broad subject which you can learn more about in an article coming soon.