Types in JavaScript
Tags: javascript, web, programming
Published: 2018-11-25 13:46:29

---
title: Types in JavaScript
published: true
description: In this post, we will talk about types of typed languages, typing and types in Javascript, some useful features.
tags: Js, web, programming
---

![Scope](./https://thepracticaldev.s3.amazonaws.com/i/o75igcwodrs7ug6hry01.jpg)


Data can be of different forms, numbers, strings, booleans etc. The type system of a programing language helps us to store various kind of data. In this post, we will talk about types of typed languages, typing and types in Javascript, some useful features.

### How do different languages handle types?

Typing in a programing language can be either static or dynamic.

- Static typing: You assign the variable its type during declaration.
- Dynamic typing: You just declare the variable, assign it any type of data during runtime. You can also change the type of data assigned to it during runtime.

### How does Javascript handle types?

Javascript is a dynamically typed language. This way you don't have to specify the types, you just declare the variables and use them as you like. While this is fun, you have to be careful as if you assign the wrong type to a variable it can lead to bugs. In Javascript, you can also check the type of the variable at runtime using the [typeof](https://bitsofco.de/javascript-typeof/) operator.

### Different types in Javascript

There are [7 primitive types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures) in Javascript. 

They are:

1. String: String allows you to store a collection of letters.
2. Number: Numerical data is useful for arithmetic operations, Javascript also has a [Math library](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) using which you can perform operations like power, absolute easily.
3. Boolean: Value can either be true or false, useful for flags.
4. Null
5. Undefined
6. Object: A collection of key-value pairs.
7. Symbol: According to MDN "Symbol value may be used as an identifier for object properties; this is the data type's only purpose". You don't need to learn about it at this point, you can take a look at it after you get other Javascript concepts clear.

Null and Undefined are values a variable can have. They both convey that the variable does not store any data. There are a few differences between the both, null is explicit, undefined is implicit. In case you want to have a variable with no data, remove the data that it has, just assign it to null. If you, create a variable and do not assign it any value it will be undefined. Another funny thing about the null type, **type of null** returns **"object"** (you can read more about it [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#Additional_information)).

### Type conversions

Often you would want to convert one type of data into another.
For these Javascript provides many helper functions. For example to convert a string to a number you can use [parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) method, and to convert a number to string you can use [toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) method.

### Truthy and falsy values

Javascript has another useful feature, it treats values as [truthy and falsy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy).
This is how the value will be treated when used in a boolean expression. A truthy value means the value is considered to be true, falsy is for false. You can then use this feature to check if a variable is defined, or not before processing it.

Along with the basic types, Javascript also has data structures like arrays, sets and maps, we will take a look at them in the next post. If you liked this post do share it.