JavaScript Data Types

gobias101
3 min readMar 16, 2022

--

For this weeks technical blog, I’d like to talk about data types and strictures in JavaScript (JS)! Data types are what type of data can be stored and manipulated within your app. While most of the built-in data-structures for all the different programming languages are similar, there are a few differences. In this blog we will talk about the data types and structures used in JS.

There are 8 different basic data types in JS, this blog will go over the main 6 used in everyday coding and give a description of each one, lets start!

  1. Numbers. The number data type can represent bpth integer and floating point numbers. To create/declare a number in JS, it’s as easy as typing the following:
let x = 45;

2. BigInt. BigInt is a data type more recently added to the JS language due to the inability of JS to represent integer values larger than 2⁵³ -1 (9007199254740991) or less than -(2⁵³ -1). For the majority of use cases, this is more than enough, but for some applications like cryptography the creator needs the use of larger (or smaller) numbers. TO use a BigInt data type in your program, you would have to declare it as you normally would a number, but with and added ’n’ at the end:

let x = 90071992547409998n;

3. String. A string is a set of characters surrounded by quotes, mainly used to encapsulate words and sentences. JS lets you use single or double quotes, it will treat the string the same. Declare a string in JS like so:

let string1 = "mike";
let string2 = 'mike wrote this blog'

You can also use backticks (``) to create a string with ‘extended functionality’ allowing you add variables into the string by wrapping it in ${…}:

let name = "Mike"
let url = `https://somewebsite.com/profile/${name}`

Now, the URL will render as: https://somewebsite.com/profile/mike

4. Boolean. Boolean is a logical type, it has only two values, true and false. It is mainly used to store yes or no values and to give results for comparisons. Here is an example of using the boolean type:

let isLesser = 7 < 4;console.log(isLesser);

If you try this in your browsers console, you can see it will output as ‘false’ letting you know that 7 is not lesser than 4.

5. Null. in JS, null is a special value representing ‘nothing,’ ‘empty,’ or ‘unknown value.’ You can use it as follows:

let name = null

6. Undefined. Undefined, unlike null, just means that the value has not been assigned yet. JS lets you declare variables without defining them right away, before the value is defined, it follows that it will be ‘undefined’ Like if you are creating a website that asks for a username, you declare the username in your code as so:

let username;

but until the user fills out a form that assigns a (typically string) value to username, it will be ‘undefined.’

As first mentioned, there are 8 data types. The remaining two are Objects/Symbols, and the typeof operator. Objects are a special type that can store a collection of different data types and deserve an entire blog on their own! The typeof operator simply allows you to check the datatype of an entry, like so:

typeof 3 //"number"typeof true // "boolean"...

Hopefully this blog gave you a good introduction and understanding to JS datatypes and how you would go about using them in your projects!

--

--

gobias101
gobias101

No responses yet