Data Types in JavaScript

Data Types in JavaScript

Introduction

We will have a look at JavaScript types and how they are a dynamic-type language. Don't freak out if you have no idea of what a "Dynamic Type" is.

In this article, we have a brief discussion of Data Types in JavaScript.

Data Types in JavaScript

Illustration

If you have any previous programming background then it would definitely help in this. Since most programming languages have some common features but JavaScript and the previous popular languages like C, C# or Java are nearly identical in terms of syntax.

One thing that is different from all the other languages is variable declaration.


In C we do it this way:

Data Types in JavaScript

So, I want to store any string type then I also need to use the var keyword.

Data Types in JavaScript

Before learning, we start with the conventional introduction in the Programming World, a Hello World application.

The following show how a "Hello World" program looks in JavaScript in HTML.

Data Types in JavaScript

And here the document.write() method is used to print anything in JavaScript. Just like we do in C with printf() and Console.Writeline() in C#.

In JavaScript, we are not concerned about Data Type declaration though we use the var keyword instead. And the Java Interpreter casts the type of data itself.

Basically, we have the following three primary data types in JavaScript:

  • Numbers
  • String
  • Boolean

So, what are the valid names for any variable?

It's quite the same but with a little more.

  • Variable name must begin with letter, $ or _ (underscore).
  • Variable can contain only letter, number, $ or _.
  • Variable names are case-sensitive.
  • Avoid keyword as your variable name.

Using Variable as Number

Data Types in JavaScript

And, your output is:

Data Types in JavaScript

Note: When we deal with a string type then the + operator acts as a concatenation operator.

You can do various mathematical operation as you have in other languages, including the following:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)

Using Variable as String

Data Types in JavaScript

Here, we have declared three variables that implicitly point to a string type.

And in the document.write() statement we have concatenated all three variables using the + operator.

Data Types in JavaScript

We can use concatenation in various ways.

Data Types in JavaScript

In this, we concatenate a string with a number that is treated as a string as well.

So, in this we have no confusion, but what if we want to concatenate a string with a number and want some number over that number.

Then, we use a special method called Number() and the string (in other words a number) in its argument.

Data Types in JavaScript

With the Number() method, you can convert any number that is in a string type to a number type.

If you get any error, then it will show a NaN error (in other words Not A Number error).

As, you can see when I a wrong value, then:

Data Types in JavaScript

The output will be like:

Data Types in JavaScript

Get some real part of JavaScript: prompt() and alert()

Alert() is special JavaScript method to popup a message box in JavaScript.

Data Types in JavaScript

With a default OK button.

And, the same as a Message Box we have a prompt() method that will popup a message box that has a Text Input area.

Data Types in JavaScript

So, we have something like this.

Data Types in JavaScript

For now, we combine both of these concepts to create a usefull examle of JavaScript.

Data Types in JavaScript

In this, we combined both of these in a single example.

Since the prompt() method always returns a value, that will be put inside that text box.

Using that, we stored that value in an input variable. And we popup that value in an alert box as in the following:

Data Types in JavaScript

After that, Ok we have:

Data Types in JavaScript

We have our correct output.

But, some strange texts is also there. Don't take it into consideration since it is handled by the browser.

Conclusion

We discussed JavaScript types, alert(), prompt() and a few basic concepts. These things are building blocks for JavaScript. In a future article we will go to another type, Arrays.