In Javascript (or any programming language), a data type is the type of value a variable holds, like a number or a string for example. There are generally seven data types in Javascript and I’ll “generally” explain all of them here, I must mention though, this will not be a tutorial on data types so if you want an in-depth definition of each data type with examples I recommend you check out javascript.info.
- String
- Number
- Boolean
- Object
- Array
- Null
- Undefined
The String: a string is surrounded/enclosed in quotes, there are three types of quotes used in Javascript.
const strng = 'this is a string'; //single quotes
const strng = "this is a string"; //double quotes
const strng = `this is a string`; //backticks
Javascript treats all the values above the same – they are all strings.
The Number : the number data type is pretty self-explanatory, its a number. Any value that is a number is a number data type, its important that the value is NOT ENCLOSED IN QUOTES for Javascript to recognize it as a number data type otherwise it will be treated as a string.
const numb = 25; //this is a number data type
const num = "25" //not a number
The Boolean: booleans are logical types – they are values that can only be in one of two conditions: true or false. There’s massive use for this data type because just like you and I, applications need to make decisions based on given conditions.
const isAwake = true;
const isAsleep = false;
Again, its important that a boolean is not enclosed in quotes otherwise Javascript treats it as a string.
The Object: all other types are called “primitive” because their values can contain only a single thing (be it a string or a number or whatever). In contrast, objects are used to store collections of data and more complex entities, so objects kinda get special treatment.
const person = {
name: "Allison",
profession: "AI researcher",
age: 29,
isMale: false;
isFemale: true;
hobbies: ["photography", "piano", "filmmaking", "reading"]
}
Above is a simple Object containing different data types defining a person , this could be a user on your website or something…
The Array: when you want to store lists or a list of data, normally you’d use an Array. With Arrays you can manipulate the data inside, manually and automatically.
const arr = [1, 2, 3, 4, 5]; //an array of numbers
const arr = ["milk", "cereal", "eggs"]; //an array of strings
const arr = ["coffee", 12, "muffins", 45, 88]; //an array of number and string data types
Similar to Objects, Arrays can contain multiple data types.
The Null: null means nothing, its a value, a value of nothing (the way I like to think of null is that it represents the value of nothingness).
const msg = null; //msg represents nothingness
Again, its important that null is not enclosed in quotes otherwise Javascript treats it as a string.
The Undefined: this one is kinda tricky, usually Javascript will return undefined if you’re trying to output something that hasn’t been defined. But Undefined itself is a data type in Javascript. You can assign Undefined to a variable (for whatever reason) and it will return undefined always, unless updated.
const msg = undefined;
I hope I “generalized” these well enough without this blog being too long.
Alright cool
Top comments (0)