Variables and Data Types in JavaScript

Variables and Data types are the basic concepts of any programming language.

First of all, variables are like containers in which we can store values in order to use them over and over again in our code. By saving values, we don’t have to write them every time we need to use them.

Data types are classifications of data. Variables can hold various data types like – numbers, strings, booleans etc. Data types are important to work with variables.

Why?

Because it doesn’t make sense to, let’s say, add numbers with words or subtract numbers from words. If you try to add 21 with the word “programming” what result will it produce?

For JavaScript, it will treat 21 as a string instead of a number and return 21programming.

Before working with JavaScript, you need to setup few things to try and test your codes. Create a folder and create two files in the folder – index.html and script.js

Now add the following to your HTML file using any code editor.

This adds the script file in the HTML file. Now when you load the HTML file in your browser, the JavaScript file will load too. You can write and test your code now.

Variables and Data Types in JavaScript

Let’s dig deep into variables and data types used in JavaScript.

Variables in JavaScript

Like we said earlier, variable can hold a value that we can use as many times we want without writing the value again and again.

Let’s see how to write a variable in JavaScript.

This stores the value “Imtiaz Rayhan” to the name variable. If we want to use this value anywhere in our code, we just call the variable name.

Now, to check if it’s working, we’ll use console.log() method.  Use it like this:

Save the script.js file with this and open index.html file on your browser. You can’t see anything yet. Right click on the page and click on Inspect Element.

Now you can see Console, click on that and you’ll see your value printed there.

Variables and Data types in JavaScript

Let’s check a variable with a number.

You should see the number 21 on your console.

In addition, variables in JavaScript are dynamic, which means they can hold different types of data.

Now, let’s check out the data types that we can use with variables.

Data Types in JavaScript

Data types are basically classification of data. Like we said earlier, data types are important for proper execution of code. There are six primitive data types in JavaScript :

  1. Numbers
  2. Strings
  3. Boolean
  4. Null
  5. Undefined
  6. Symbol

Primitive data types are single data types. That means primitives can hold only one value.

Another data type is Objects. Objects can hold multiple values.

Numbers

JavaScript numbers are always floating point numbers. That means they always have decimals even if we don’t see them or define them.

For example, having a 10 means having 10.0 as the value.

In other programming languages, you may have different data types for integers or decimals.

These are some examples of number data types. You don’t need any special syntax for numbers, like for strings you need to use quotes.

Therefore, if you use quotes with numbers, they will be considered as a string.

Furthermore, you can do regular math with number data types.

Strings

Strings are series of characters and any text is represented by a string.

A string is usually wrapped in quotes. You can use double quotes or single quotes, whichever you like. Also, you can use quotes inside a string but both end of the string must match.

Hence, if you use single quote to surround the string, you can use double quotes inside the string. And if you use double quotes to surround the string, you can use single quotes inside the string.

Boolean

Boolean is a logical data type that can take one of the logical value of true or false.

To make it simple, boolean is either true or false. And this data type is mostly used to check if statements or conditions are true or false.

Let’s look at an example:

Booleans are used in all programming languages when a value will change between true or false.

Undefined

A variable with no assigned value yet, is undefined in JavaScript.

We declare a variable but we don’t assign any value to it. It will return undefined.

We can also empty a variable using undefined as the value.

If we declare a variable without using var, it’ll undefined too.

Null

A NULL simply means nothing.

It’s a value that represents nothing which means, currently that variable does not have any value but it will have later on.

In the above example, null is assigned to a variable nothing. It means we have defined a variable but have not assigned any value yet, so value is absent.

Over to You

So, these are the basics of variables and data types in JavaScript. There are other data types like objects, arrays.

Those are a bit complicated than these. I’ll talk about them in details in our future posts.

Let me know through comments if you have any questions.

Check Out These Too
No related posts for this content

Click Here to Leave a Comment Below

zmean.com

When adding a number and a string, JavaScript will treat the number as a string.

Reply
Leave a Reply: