Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 20, 2021 07:34 am GMT

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

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


Original Link: https://dev.to/thenerdydev/beginner-javascript-7-introduction-to-data-types-1pm1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To