JavaScript Data Types

lenacodes

Lena

Posted on March 6, 2023

JavaScript Data Types

Data Types refer to the types of data that can be stored in a variable. They include primitive and non-primitive data types.

The primitive data types are:

String
Number
Boolean
Undefined
Null
Symbol
The non-primitive data type is:

An object can be an array, function, date , object and Regex (Regular expression).

The major difference between the primitive and non-primitive data types is that in primitive data types, only one value can be stored while more than one value can be stored in the non-primitive type.

For the purpose of this post, we will only focus on string, number, boolean, undefined and null.

String

A group of characters surrounded by single quotes (‘’), double quotes(“”), backticks or String() method.

let str1 = 'String in single quote.';
let str2 = "String in double quote.";
let str3 = `String in backticks.`;
let str4 = String('Creating a string with String().');
Enter fullscreen mode Exit fullscreen mode

Number

The number can be an integer (positive or negative), a floating point value, exponential value, Infinity or Nan (not a number).

const num1 = 1; // Positive integer
const num2 = -10; // Negative integer
const num3 = 24.3; // Floating point value
const num4 = 10e2; // Exponential value: 10*100
const num5 = 5 / 0; // Infinity
const num6 = 'hello' * 5; // Nan
let num7 = Number(10);
Num7 = Number(5);
Enter fullscreen mode Exit fullscreen mode

Boolean

This is a logical data type that has two values true and false. It can be used for comparison of variables or to check a condition.

let checker = true;
const a =5, b = 9;
if (a < b) {
  // if a is less than b
  checker = true;
}
else {
  checker = false;
}
a == b; // a is equal to b returns false
a != b; // a is not equal to b returns true
Enter fullscreen mode Exit fullscreen mode

Undefined

This type of data is a variable where the variable has been declared but remains undefined. A value has not been assigned.

let firstName; // Declaration
console.log(firstName); // Undefined
firstName = 'John'; // Assign the string John
console.log(firstName); // John
Enter fullscreen mode Exit fullscreen mode

Null

The null data type refers to a value that means the value is absent or empty.

let x = null;
console.log(x) // null
Enter fullscreen mode Exit fullscreen mode

If this was helpful to you, would appreciate if you like this post and share.

Thank you for reading.

💖 💪 🙅 🚩
lenacodes
Lena

Posted on March 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

JavaScript Data Types
javascript JavaScript Data Types

March 6, 2023