Beginner JavaScript – 7 – Introduction to Data Types

Hey everyone ??,

In this article, let us discuss about the basic introduction to Data Types in JavaScript. This is the seventh part of my Beginner JavaScript Series on Dev.

Data Types in JavaScript – A complete picture

Any variable tha…


This content originally appeared on DEV Community and was authored by The Nerdy Dev

Hey everyone ??,

In this article, let us discuss about the basic introduction to Data Types in JavaScript. This is the seventh part of my Beginner JavaScript Series on Dev.

Data Types in JavaScript - A complete picture

Alt Text

Any variable that we define in JavaScript has a data type.

For example, if you have a sentence then that sentence is a series of characters so in the programming language, it is referred as a string of characters. A word itself is a string as well because it is composed of several characters like 'cricket', 'javascript' etc. Any numerical figure will be of number data type.

So these are the data types that we have in JavaScript and we will understand briefly about them in this post.

  1. Number
  2. String
  3. Boolean
  4. Null
  5. Undefined
  6. Symbols
  7. Objects

Numbers

In JavaScript, numbers are primitive data types. For example,

const a = 4;
const b = 34.44;

Unlike other programming languages, here you don't have to specifically declared for integer or floating values using int, float as we do in C++, Java etc. With the variables of this data type, you can perform operations like addition, subtraction, multiplication, exponentiation etc.

String

A JavaScript string represents a series of characters like "Good Morning". A string can be any text inside double or single quotes:

const stringOne = "Good Morning";
console.log(stringOne); // logs "Good Morning" to the console

We will learn more about strings in a separate post.

Boolean

A JavaScript Boolean represents one of two values: true or false.
So using a boolean, you can check the truthiness or false state of some expression. We will learn more on Booleans in a separate post. But let us see a real quick example to understand a boolean in a nutshell.

const day = "Monday";
const isMonday = day === "Monday"; // If day is Monday, this expression will resolve to true else it will resolve to false. 
console.log(isMonday); 

Null

The value null represents the intentional absence of any object value. The meaning of the word null is void or absence of something It is one of JavaScript's primitive values and is treated as false for boolean operations. There is a little quirk here despite null representing the absence of a value yet the data type for it is object in JavaScript.
You can assign null to a variable to denote that currently that variable does not have any value but it will have later on. A null means absence of a value.

console.log(typeof null);          
// "object" (not "null" for legacy reasons)
let someVariable = null;
console.log(someVariable); // null 

Undefined

It is the default value of a variable when it is just declared but not assigned an initial value.

let name; 
console.log(name); // logs undefined to the console 

let age = 21;
age = undefined; 
console.log(age); // logs undefined to the console.

Comparing null and undefined

When comparing null and undefined using ==, they are considered equal. For example,

console.log(null == undefined); // logs true to the console. 

The reason for getting true in the console for the above code snippet is that , == compares values by performing type conversion. Both null and undefined return false. Hence, null and undefined are considered equal.

However, when comparing null and undefined with strict equality comparison using ===, the result is false.

console.log(null === undefined); // logs false to the console. 

Symbols

A symbol represents a unique identifier.
A value of this type can be created using Symbol():

const newId = Symbol();
console.log(newId); 

You could optionally also pass an argument to the Symbol to provide it some description.

Symbols are guaranteed to be unique. So even if you create many symbols with the exact same description, they are still considered as different values. The description is kind of a label and it does not affect anything.

Object

It is also one of the data types that we have in JavaScript. An object is a collection of key-value pairs. So each object that you and I create can be represented using the Object Literal Notation and it takes on property names along with their corresponding values. So let us see an example for objects as well.

const user = {
  firstName: "Alex",
  lastName:"Jones",
  age: 21,
  eyeColor: "blue",
  programmingLanguages: ['Java','Python','JavaScript','Ruby'],
  isDeveloper: true
};

Check my video on Introduction to Data Types to get more understanding on these:

So this is it for this one.

If you are looking to learn Web Development, I have curated a FREE course for you on my YouTube Channel, check the below article :

Spare 2 Hours ? If so, utilize them by creating these 10 JavaScript Projects in under 2 Hours

?? Follow me on Twitter : https://twitter.com/The_Nerdy_Dev

?? Check out my YouTube Channel : https://youtube.com/thenerdydev


This content originally appeared on DEV Community and was authored by The Nerdy Dev


Print Share Comment Cite Upload Translate Updates
APA

The Nerdy Dev | Sciencx (2021-06-20T07:34:18+00:00) Beginner JavaScript – 7 – Introduction to Data Types. Retrieved from https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/

MLA
" » Beginner JavaScript – 7 – Introduction to Data Types." The Nerdy Dev | Sciencx - Sunday June 20, 2021, https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/
HARVARD
The Nerdy Dev | Sciencx Sunday June 20, 2021 » Beginner JavaScript – 7 – Introduction to Data Types., viewed ,<https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/>
VANCOUVER
The Nerdy Dev | Sciencx - » Beginner JavaScript – 7 – Introduction to Data Types. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/
CHICAGO
" » Beginner JavaScript – 7 – Introduction to Data Types." The Nerdy Dev | Sciencx - Accessed . https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/
IEEE
" » Beginner JavaScript – 7 – Introduction to Data Types." The Nerdy Dev | Sciencx [Online]. Available: https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/. [Accessed: ]
rf:citation
» Beginner JavaScript – 7 – Introduction to Data Types | The Nerdy Dev | Sciencx | https://www.scien.cx/2021/06/20/beginner-javascript-7-introduction-to-data-types/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.