Are you a beginner in JavaScript or just starting to learn the language? If so, you'll probably have come across the concept of data types. There are 8 data types in Javascript. Of these 8, 5 are the basic ones you'll use most often, but knowing all 8 is worth it. Here are the 8 data types we have in Javascript:
Strings
Numbers
Boolean
BigInt
Undefined
Null
Symbol
Object
Let's look at these datatypes in more detail
- Strings: JavaScript strings are for storing and manipulating text. Strings are like the building blocks of written information in JavaScript. You'll use them to create all sorts of text-based content. A JavaScript string is composed of zero or more characters written inside quotes. Usually, single quotes (' '), double quotes (" "), or backticks (``) are used to represent strings. Mixing quotation marks can cause errors. Use either single or double quotes consistently. Examples of strings include:
"Hello Everyone"
'My Name is Nenye'
`How are you doing today?`
'123'
You could also read more on strings from the W3Schools documentation on strings
- Numbers: Numbers, as the name implies, are numeric figures. Numbers can be whole numbers or decimals. Unlike some other programming languages, JavaScript treats all numbers the same. JavaScript doesn't have separate categories for different number types. Examples include:
let number1 = 20
let number2 = 20.5
You can read more on numbers from the W3Schools documentation on numbers
- Boolean: Booleans can have only two values: true and false. Booleans are often used in conditional testing. Take a look at the following examples:
let x = 3
ley y = 3
let z = 7
x == y //returns true
x == z //returns false
You can get more use cases of the boolean data type from the W3Schools Documentation
BigInt: BigInt is a datatype used for numbers that are too big to be represented by a normal javascript number. You create a BigInt number by either adding "n" to the end of the number or by calling the BigInt() function and giving it a number or string value.
Examples:
let number3 = BigInt("253840926484020286546272053893483920")
let number4 = BigInt(82670303482518982538201019233459753)
let number5 = 820038476483029846202n
You can read more on the BigInt data type from the MDN Web Docs
- Undefined: Undefined indicates that a variable has not been assigned a value. Here's an example:
let x;
In the example above, the variable x is undefined because it doesn't have a value. A function that returns no value will also return undefined.
You can explore other instances when undefined is returned from the MDN Web Docs
- Null: Think of null as a placeholder that means "no value." It's like a reserved spot that isn't holding anything yet. You might use null to show that a variable doesn't have a value assigned to it or that a function doesn't return any data. This is an example of a null value:
let emptyBox; // Declares a variable but doesn't assign a value (null by default)
NB: Undefined
and null
might seem like twins, but they're not the same! Null
says "no value on purpose," while undefined
says "haven't gotten a value yet."
Null is like an empty box you made on purpose, while undefined is a box that hasn't been filled yet.
Symbol: A symbol is a new datatype introduced in version ES6/ES2015. It is a unique and unchangeable value that can be used as the key of an object. The purpose of using a symbol is to create a unique key that will not clash with keys from other parts of the code. A symbol can be created using the Symbol() function and passing a name or description.
let name = Symbol("name") //name is a symbol with description "name"
You can read more on symbols from Javascript.info
Object: An object is like a container that can hold a collection of things. Imagine a box where you can store and label different items for easy access. Objects store information in key-value pairs. The key acts like a label, and the value is the data you want to store. You can have many key-value pairs within a single object.
const person = { // key: value firstName: "Alice", lastName: "Smith", age: 30 }; // Accessing information using the key console.log(person.firstName); // Outputs "Alice"
You can learn more about objects from the W3Schools Documentation
To recap, here's a rundown of the essential concepts we explored about data types.
JavaScript has different data types to represent various kinds of information. This helps keep your code organized and avoids errors.
Primitive data types include numbers, strings, booleans, null, and undefined. These hold single values.
Objects are more complex and can store collections of information using key-value pairs.
Learning data types is like learning a new language. The best way to solidify your understanding is to experiment and practice! Here are some ideas:
Play around with the data types in your code. Try creating variables of different types and see how they behave.
Write small programs that test your understanding. For instance, try a program that asks the user for their name (string) and age (number) and then greets them.
Challenge yourself with data-type puzzles. Look for online exercises or coding challenges that involve manipulating data types.