DEV Community

Kartik Malik
Kartik Malik

Posted on

Types in JavaScript

Scope

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 operator.

Different types in Javascript

There are 7 primitive types 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 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).

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() method, and to convert a number to string you can use toString() method.

Truthy and falsy values

Javascript has another useful feature, it treats values as truthy and falsy.
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.

Top comments (0)