Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




JAVASCRIPT - TYPE CASTING


So far we have seen, how to add two numbers that are of the same type.


i.e. We have added two numbers that are of Data Type number.


Example :



<html>
<body>
<script language = "javascript" type = "text/javascript">
   		
   	var x = 5 
	var y = 6
	var z = x + y
	document.write("The added value is : ",z)  
		
</script>      
</body>
</html> 


Output :



  The added value is : 11

So, if we see the above output, the Data Type of x and y is number because both have an integer value in it.


var x = 5
var y = 6

Similarly, if we see the Data Type of the variable z, it is also number. Because z holds the added value of the variables x and y. Which is also a number(i.e. 11).


Now, there might also be a scenario where we have assigned a number in String format. And want to add them with a number.


Let us see in the below example,


Example :



<html>
<body>   
<script language = "javascript" type = "text/javascript">
   		
   	var x = 5 
	var y = "6"
	var z = x + y
	document.write("The added value is : ",z)  
		
</script>      
</body>
</html> 


Output :



  The added value is : 56

Well! If you see the output, it is 56 instead of 11.


So, we have a number i.e. 5,


var x = 5

And a string i.e. "6"(Since "6" is in double quotes, it is treated as a string).


var y = "6"

And when we try to add them,


var z = x + y

The result is 56. Because "6" is a String, it is just concatenated with 5 instead of adding it.


And to actually add the values, we need to convert the string to number type.


Converting a string Data Type to a number Data Type


Example :



<html>
<body>   
<script language = "javascript" type = "text/javascript">
   		
   	var x = 5 
	var y = Number("6")
	var z = x + y
	document.write("The added value is : ",z)  
		
</script>      
</body>
</html>


Output :



  The added value is : 11

Now, if you see the output, we got the desired result.


And what we have done is, used the Type Convertor Number() to convert the String to number.


var y = Number("6")

And 6 is treated as a number and we were able to add them.


Here, Number() is a type converter that is used to convert a String to a Number.


Now, let us say there is a String. Say a name John.


And we want to convert the name John to a String. Well! Thats not possible.


So, are we suppose to get an error? Or something else?


Let us see in the below example.


Example :



<html>
<body>   
<script language = "javascript" type = "text/javascript">
   		
	var x = Number("John")
	document.write(x)  
		
</script>      
</body>
</html> 


Output :



  NaN

And we got a weird output NaN.


The output actually means that the String "John" cannot be converted to a number because it is Not a Number.


And that is the abbreviation of NaN - Not a Number


Next, let us see, how can we convert a Number to a String.


Converting a number Data Type to a string Data Type


Example :



<html>
<body>
<script language = "javascript" type = "text/javascript">
   		
   	var x = 5 
	var y = String(6)
	var z = x + y
	document.write("The concatenated value is : ",z)  
		
</script>      
</body>
</html>


Output :



  The concatenated value is : 56

So, in the above example, we have a number 5.


var x = 5

And we also have a number 6.


And this time we want to get the output as 56.


So, we simply convert the number 6 to a string.


var y = String(6)

And now that 6 is a string. When we add them,


var z = x + y

We get the output as 56.


The concatenated value is : 56

Converting a boolean Data Type to a Number Data Type


Example :



<html>
<body>
<script language = "javascript" type = "text/javascript">
   		
   	var x = Number(true)
	var y = Number(false)
	document.write("The coverted value of true is ",x," and false is ",y)  
		
</script>      
</body>
</html> 


Output :



  The coverted value of true is 1 and false is 0

So, in the above code, we have value true and false of Boolean data type.


Now, if we try to convert the boolean values true and false to integer.


var x = Number(true)
var y = Number(false)

The boolean value true becomes 1 and false becomes 0.


And we get the below output.


The coverted value of true is 1 and false is 0