JavaScript Data Types
Tags: javascript
Published: 2023-03-03 16:33:17

---
title: JavaScript Data Types
published: true
date: 2023-01-07 08:00:00 UTC
tags: JavaScript
canonical_url: https://www.90-10.dev/js-data-types/
---

Understanding data types is an essential aspect of writing effective JavaScript code.

## Primitive Data Types

There are six primitive data types - they are simple and immutable i.e. their values cannot be changed once they are created. Primitive data types are the building blocks of JavaScript and are used to represent basic values:

1. **String** -  text (a sequence of characters), such as "hello world". String manipulation functions, such as `toUpperCase`, `toLowerCase`, `split`, `slice`, and more are available. Strings can be concatenated using the `+` operator.
2. **Number** - a numeric value, such as 42. JavaScript provides a rich set of mathematical operations, such as addition, subtraction, multiplication, division, modulo, and more. Special numeric values such as `Infinity` and `NaN` are supported.
3. **Boolean** - a logical value, either true or false, used to represent the truth value of an expression often used in conditional statements, loops, and other control structures. JavaScript provides logical operators such as `&&`, `||`, and `!` to manipulate boolean values.
4. **Undefined** - a variable that has been declared but has not been assigned a value. Undefined values can lead to bugs in your code, and it is important to always initialize variables before using them.
5. **Null** - a variable that has been explicitly assigned the value null - represents the absence of any object value, often used to indicate that a variable or object property does not have a value.
6. **Symbol** - (introduced in ECMAScript 6) a unique identifier that can be used as the key of an object property. Symbols are used to create unique identifiers for object properties.

## Complex Data Types

1. **Object** - a collection of properties, where each property consists of a key-value pair. Objects can be created using the object literal syntax, which looks like this: `{}`. They are used to represent more complex data structures in JavaScript and can contain other objects, functions, and even arrays.
2. **Function** - a block of code that can be called by other parts of the code. They can be created using the function keyword, and can take parameters and return values. Functions are used to create reusable blocks of code for  custom logic, perform calculations, manipulate data, and more. 

## Type Coercion

An important JavaScript feature in the context of data types type coercion - the language can automatically convert one data type to another. It can lead to unexpected behavior in your code if you are not careful. For example, the `+` operator can be used to concatenate strings, but it can also be used to add numbers. If you try to add a string and a number, JavaScript will convert the number to a string and concatenate them together. To avoid unexpected coercion, it is important to always use strict equality operators (`===` and `!==`) and to explicitly convert data types when necessary.

## Take away

JavaScript data types are foundational and provide the basis for building more complex data structures, algorithms, and programming patterns.  By understanding them intimately, you can write better code and avoid common pitfalls.

As a JavaScript developer, you will encounter many situations where you need to manipulate data types - it is important to have a clear understanding of the different data types and their properties.