Do you know JavaScript series
Download source code
Introduction
Here, we are starting a series of articles related to JavaScript. As this is the first part of the series, here we are going to see some basics of JavaScript which you may have forgotten already or you may not be aware of. You can always see my other posts related to JavaScript here. We will be using Visual Studio for our development. Let’s begin.
Basically, JavaScript is a programming language of HTML and the Web. Nowadays, we can create any kind of application using JavaScript. If you are totally new to JavaScript, I strongly recommend you read some basics here.
Background
Recently, I was assigned to a training task where I needed to train one of my colleagues who is a fresher and totally new to the programming field. I had covered almost all the topics in my training and I am sharing a few of them here. I hope you will like this.
Create a HTML page
As we already said, JavaScript is a programming language of HTML and the Web, we need a page to work with JavaScript. Let’s create it first.
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8" />
- </head>
- <body>
- </body>
- </html>
Now, create a JS file and include it in your page.
- <script src="JavaScript.js"></script>
Let’s begin our tutorial
Any idea what will be the output of the preceding lines of code?
- console.log(2 + 2);
- console.log(2 + "2");
- console.log(2 + "2" + 2);
- console.log(2 + 2 + "2" + 2);
- console.log(2 + 2 + "2" + 2 + 2);
- console.log(2 + "2" + 2 + "2" + 2 + 2);
- console.log("2" + 2 + "2" + 2 + 2 + 2 + 2);
Once you run your application, press CTRL+SHIFT+J if you are opening your application in Google Chrome or CTRL+SHIFT+K if it is Mozilla, or press F12 if in IE. This will open the browser console. Now, check the output. Is it the same as you thought?

Javascript_tutorial_output_1
If you noticed well, console.log(2 + 2 + “2” + 2 + 2); returned 4222. The First 2s are added and when it comes to the third digit, the type of the result is being changed to a string, so the string concatenation is happening. This is because of the dynamic nature of JavaScript.
So, you can always add different types of values to the same variable in JavaScript. The preceding codes will always be working perfectly.
- var a = 1;
- a= = "Sibeesh";
Now, let's take a simple test. What will be the output of the preceding code?
- var b;
- console.log(b);
Yes, you are right! It will return undefined as we have not assigned any values to the variable b. Have you ever checked the type of an undefined variable?
- console.log(typeof(b));
The result will be undefined. Now, what about the preceding codes?
- console.log(typeof(undefined));
- console.log(typeof(typeof(b)));
- console.log(typeof(String(b)));
- console.log(typeof({a:b}));
- console.log(typeof(null));
It can give you an output as below.
- undefined
- string
- string
- object
- object
Here, one thing is worth noticing- typeof(typeof()) will always return string. And {a:b}, null are actually objects so the typeof will return the type as an object. Let’s write a simple program now.
- var a=1;
- var b=a;
- a=2;
- console.log(b);
- console.log(a);
See how simple it was. Any idea what would be the output of that? If your answer is 1 & 2, then you are right. Because the variables we just created are primitive types. In primitive data types, the values are saved directly on the variable. If you are coming from the background of C#, you can understand the value typed variable.
Now, we have one more data type, which is a referenced variable where values are stored as a reference, not as direct values. An example of a referenced type variable is Object. Let’s go on to the next topic which is an overview of JavaScript Objects, Do you know how to create an object?
- var myName = {firstName: "Sibeesh", lastName: "Venu"};
- console.log(myName);
- console.log(JSON.stringify(myName));
- console.log(myName.firstName);
- console.log(myName.lastName);
Here, myName is an object. You can always use JSON.stringify() to make your object to a string format and JSON.parse() to make your string to object. So the above code will give you an output as follows.

Javascript_tutorial_output_2
An object is a collection of key-value pairs like we see in the above object, firstName is key, and “Sibeesh” is value. How can we take any values from an object? We can always access a property from an object using dot(.) operator as follows.

Naveen BishtPosted Mar 10, 2017, 9:19 AM
Can you explain == and === also prototyping and inheritance please. also what is diffreance between new and object.create
Vaibhav DeshmukhPosted Mar 7, 2017, 1:14 AM
Nice one Sibeesh Venu.Its very helpful article and yes it's 100% Eligible for Featured article. :)
Sourabh SomaniPosted Mar 3, 2017, 5:07 AM
Sibeesh Venu Nicely Explain Bro but there is one mistake..................................................... var a = 1; a= = "Sibeesh"; (this will give error)
Subhashkumar YadavPosted Mar 3, 2017, 12:50 AM
Nice Work Bro.............
Sarath MayaPosted Mar 2, 2017, 1:05 PM
I am confused on "create an object with a function inside". And I run this line of code it shows error. Could you elaborate in detail?