Java Script Basic


In this blog I am going to discuss some important and basic concepts of Java Script.

Let’s see one by one;

In JavaScript we declare variable using var keyword; we may assign the variable or we may not.

Key Capsule 1:-

So what happen when we declared

var name;

Capsule 1:- Here name stores by default undefined;

if we print using console.log(name)

o/p :- undefined.

Key Capsule 2:-

var name=”Surya”

    var name;

console.log(name)

What you think what will be the out put ?

O/P :- “Surya”

Capsule 2 :- In java script although I declare the variable once again it won’t loose the value.

Key Capsule 3:-

var result=”sun”+2+3;

console.log(result)

result=2+3+”sun”;

console.log(result)

what you think what is the out put ???

O/p:- “sun”23

5”sun”

Capsule 3:- In java script if any numeric values comes after the string then the Plus (+) operator work like as a concatenation operator where as if the numeric value comes before the string then plus (+) operator works and it show addition of number then it concatenate the string.

Key Capsule 4:-

var studnetName=”Suryakant”;

var studentName=”Rajanikant” ;

console.log(studentName);

var studnetName=”Rahul”;

var studentname=”Raj”;

console.log(studnetName);

console.log(studentname);

What will be the out put ??

O/P:- “Rajanikant”

“Rahul”

“Raj”

Capsule 5:- Why so ?? Because the JavaScript variable are case sensitive; so when I write studnetName and strudnetname both are different variable.

Remember all variable in JavaScript are called as identifier and each variable is an unique identifier.

Key Capsule 6 :-

How to print a string like this;

“The name is “Surykant”; he is a blogger.”

I want to print suryakant with the double quot.

To do this in javascript we have different escape character which can be use to achieve this.

We can write;

var data=”The name is \”Suryakant\”; he is a blogger”.

Same for single quto;

var data=”It\’s looking good.”

Most Important Key Capsule 7:-

Difference between == and === operator

“== “ operator is used to check directly the value where as “===” equality operator check the equality and types.

See bellow examples;

Test Case 1:-

var x=”surya”;

var y= “surya”;

console.log(x==y)

console.log(x===y)

o/p- true

Note :- Here the output true for both the case because both are storing same value and both are of same type

Test Case 2:-

var x=”surya”

var y=new string (“surya”)

console.log(x==y);

o/p- true

Scenario:- Here some of us might think that why it returns true ?

This question also came to my mind; because the second one is reference type; as we know inside reference type variable it stores the address of that value i.e “surya”.

So how both are equal then ??

Here important thing to understand that what actually happen when we comparing the value using double equality operator (“==”).

When we using double equality operator (“==”) to compare then this operator perform a type conversion while comparing two things; so in the above example when we compare x and y, although y is a object type it is converted to string type while comparing so both are get same type with value so it returns as true.

Where as triple equality operator (“===”)  never do any type of conversion while comparing; it only check the type and value if both are same then it returns true other wise returns false.

Key To Remember :– So if we check any two reference/object type variable using triple equality operator (“===”) then it always return false; same for double equality operator (“==”) also return false; but if both are reference type pointing to same location then “===” operator return true.

Confused ????

Let’s clear; both reference type/object type pointing to same location means;

var obj=new string(“surya”);

var obj1=obj

Now if we check; obj===obj then we get output as true.

Capsule 7 :- What we learn form the above :-

Double equality operator(“==”) do conversion before checking while triple equal to operator(“===”) never do any conversion before checking it just check the type and value.

examples :-

var a=1;

var b=”surya”;

var c=new string(“surya”);

Now; a===a   // True ;   b===b // True;  c===c   //True

Again if i declare a new object type variable;

var d=new string(“surya”);

Now;  c===d  // False

Note :- Although ‘c’ and ‘d’ are the same type still it return false because after checking the type it will check the value inside it; both are pointing to different location so both have different value so it return false.

if we write; c==d

Note :- Now what happened both are converted to reference type; and due to both pointing different location; so it return false.

Key To Remember :- Due to the above reason double equality operator (“==”) called as loose equality operator where as triple equality operator (“===”) called as  strict equality operator.

Test Case 3:-

var x=”surya”

var y=new string (“surya”)

console.log(x===y);

o/p:- false

Note :- Here the output is false because “===” operator is used to check type and both having different types so it return false.

Test Case 4:-

var x=new string(“surya”);

var y=new string (“surya”);

console.log(x==y);

o/p:- false

console.log(x===y)

o/p:- false

What is value type and reference type in JavaScript ?

Ans:- In java script data types are classified into two types such as value type and reference type.

Under value type we have data type like;

Int, string,number,Boolean,null and etc.

Where as under reference type we have data type like;

Function,array,object.

Combine this all we said all these are object type.

Note:- Value types are directly stored value in side the variable where as reference type are stored the address of the object and that address pointing to the actual value location.

Key Capsule 8 :

In Java script we have different global methods for Number such as;

  • Number()
  • parseInt()
  • parseFloat()

Important Test Case:-

Test Case 1 :- parseInt(“10 Year”) //O/P- 10

Test Case 2 :- parseInt(“Year 10”) //O/P – NaN

Key Capsule 9:-

In JavaScript we can define an array by using bellow syntax;

Var stdArr=[];

Note:- stdArr is a special type of object which store different type of object inside it.

Join Method in Array:-

In JavaScript the join method is used to convert the array into string but we can specify separator inside join method.

For eg:- var arr=[“Ram”,”Hari”,”Madhu”]

arr.join(“*”);

so the o/p Ram * Hari * Madhu.

Key Capsule 10:-

In JavaScript we use a key word called as “use strict”.

This is indicate that we can not use a variable without declaring it. So if we use a variable without declaring it then it’s gives you an error.

Note :- Use strict can be write at the top of the script.

Use strict

a=2+3;

console.log(a) // Error because we did not declare the variable a.

 

Hope above concept helps you in understanding the basic concept; will see more on coming days.

Thanks for reading; have your feedback in comment box if you have any doubt in any test case.

#Share2Know #Learn Basic #JavaScript