Basics of JavaScript: Part 4

This is Part 4 of the Basics of JavaScript tutorial. In this part we will learn how to work with strings and numbers.

The following are the topics we have already discussed:

  1. Basics of JavasSript: Part 1
  2. Basics of JavasSript: Part 2
  3. Basics of JavasSript: Part 3

This article discusses the following topics:

  1. How to concatenate two string values
  2. How to concatenate string and a number
  3. How to add two numbers
  4. How to subtract two numbers
  5. What happens when we subtract a string with a number
  6. Various methods to convert string to number

How to concatenate two string values

To concatenate a string, we use the “+” operator as in the following:

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7. //to create a variable we use var keyword  
  8. var firstValue = 'Welcome to ';  
  9. var secondValue = 'JavaScript Tutorial';  
  10.   
  11. //to concatenate a variable we use +  
  12. alert(firstValue+secondValue);  
  13. </script>  
  14.     </body>  
  15. </html>  


How to concatenate string and number

Example 1

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7.             var myFirstValue = 'My age is ';  
  8.             var mySeondValue = 33;  
  9.             alert(myFirstValue+mySeondValue);  
  10.         </script>  
  11.     </body>  
  12. </html>  

In the preceding HTML, we have two variables, one contains a value of type string and the other contains a value of type integer.

When we concatenate a string value and a number, the number is converted into a string.


Example 2

In this example we will prove if an integer value is converted into a string and when we concatenate a number with a string.

  1. <script type="text/javascript">  
  2.    var myFirstValue = '22';  
  3.    var mySeondValue = 33;  
  4.    alert(myFirstValue+mySeondValue);  
  5. </script>  

So, even after passing a string of numeric value, both the values are concatenated.


How to add two numbers

To add a number we use the “+” operator as in the following:

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7.             var fNum = 3;  
  8.             var sNum = 2;  
  9.             alert(fNum+sNum);  
  10.         </script>  
  11.     </body>  
  12. </html>  

How to subtract two numbers

To subtract two numbers we use the “-” operator as in the following:

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7.             var fNum = 3;  
  8.             var sNum = 2;  
  9.             alert(fNum-sNum);  
  10.         </script>  
  11.     </body>  
  12. </html>  

Now you might be wondering, OK to add two numbers, the value must be numeric and if we try to add a string value and a number, the number is converted into a string. But what if we try to subtract a string value from an integer or an integer value from a string? Still the number will be converted into a string and if the number is converted to a string, how will the number and string be concatenated? We know that to concatenate two variables or values we need to use the “+” operator but here we are using the “–“ operator.

So, let's look at an example.

What happens when we subtract a string by a number

Example 1

In the first example we will see what happens if we try to subtract a text from a number.

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7.             var firstValue = 'Hello';  
  8.             var seconValue = 10;  
  9.             alert(firstValue - seconValue);  
  10.         </script>  
  11.     </body>  
  12. </html>  

In the preceding HTML, we have two variables. One holds a value of type string and the other holds a value of type integer.

In the alert function we are subtracting firstValue by secondValue.

Open the file in a browser.

We got the output as NaN.

This NaN states Not a Number. We will explain this NaN in just a bit.

For now let's look at another example.

Example 2

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <script type="text/javascript">  
  7.             var firstValue = '12';  
  8.             var seconValue = 10;  
  9.             alert(firstValue - seconValue);  
  10.         </script>  
  11.     </body>  
  12. </html>  

Here we have the same HTML document. The only change we made here is the firstValue value. This time we initialized a numeric string value.

Open the file in a browser.



Look at the output we got. This time the firstValue is converted into an integer and the “-” operator subtracted the firstValue by the secondValue and that gives us the value 2.

NOTE

Whenever we use “+” to add/concat string or a numeric string to an integer, the integer value is converted into a string whereas when we use the “–“ operator to subtract a string value from an integer value the result will be NaN because the string value that we pass was unable to be converted into an integer. But when we subtract a numeric string value with an integer, the numeric string value is converted into an integer and we get the result.

Methods to convert string into a number

The first thing we need to do is to create a UI where we can pass two numbers and get the sum of it.

  1. <html>  
  2.     <head>  
  3.         <title></title>  
  4.     </head>  
  5.     <body>  
  6.         <form>  
  7.             <table>  
  8.                 <tr>  
  9.                     <td>  
  10.                         <label>First Number </label>  
  11.                     </td>  
  12.                     <td>  
  13.                         <input type ="text" id ="txtOne"/>  
  14.                     </td>  
  15.                 <tr/>  
  16.                 <tr>  
  17.                     <td>  
  18.                         <label>Second Number </label>  
  19.                     </td>  
  20.                     <td>  
  21.                         <input type ="text" id ="txtTwo"/>  
  22.                     </td>  
  23.                 <tr/>  
  24.                 <tr>  
  25.                     <td>  
  26.                         <label>Sum </label>  
  27.                     </td>  
  28.                     <td>  
  29.                         <input type ="text" id ="txtSum"/>  
  30.                     </td>  
  31.                 <tr/>  
  32.                 <tr>  
  33.                     <td colpsan="2">  
  34.                         <input type="button" value="Sum"/>  
  35.                     </td>  
  36.                 </tr>  
  37.             </table>  
  38.         </form>  
  39.         <script type="text/javascript" src="script/script.js"></script>  
  40.     </body>  
  41. </html>  
Open the file in a browser.



Now the next thing we need to do is create an external JavaScript file with the name “script.js”.

In the JavaScript file we need to retrieve the user input from both the TextBox control, pass the result to the third TextBox control and for that we can use the getElementById method of the document object and from that we can retrieve and assign the TextBox value.

  1. function add()  
  2. {  
  3.    var fnum = document.getElementById('txtOne').value;  
  4.    var Snum = document.getElementById('txtTwo').value;  
  5.    document.getElementById('txtSum').value = fnum + Snum;  
  6. }  

Pass the preceding JavaScript function to the button's onClick event.

  1. <input type="button" value="Sum" onclick="add()"/>  

Open the file in a browser.

Pass the first and second numbers.



Click the Sum button.



Look at the output we got, rather than adding these two numbers the “+” operator is concatenating the numbers.

Reason:

The input type of our TextBox is text and the value property is returning the number in a string format. So, in order to add the numbers we need to first convert those numbers from a string to an integer.

To convert a string to an integer we can use the ParseInt(value) function or Parsefloat(retain decimal).

  1. function add()  
  2. {  
  3.    var fnum = parseInt(document.getElementById('txtOne').value);  
  4.    var Snum = parseInt(document.getElementById('txtTwo').value);  
  5.    document.getElementById('txtSum').value = fnum + Snum;  
  6. }  
Now add the numbers.



Now what will happen if we pass a text instead of a number?



We got the sum value as NaN because the parseInt function was unable to parse “ww” into an integer.

To check whether the specified value in the text is a number or not we can use the isNaN function.

  1. function add()   
  2. {  
  3.     var fnum = parseInt(document.getElementById('txtOne').value);  
  4.     var Snum = parseInt(document.getElementById('txtTwo').value);  
  5.   
  6.     //if the number isNaN, it will return true else false  
  7.     if (!isNaN(fnum) && !isNaN(Snum))   
  8.     {  
  9.         document.getElementById('txtSum').value = fnum + Snum;  
  10.         //here we are checking if the value not isNaN then we are adding the numbers  
  11.     }  
  12.     else   
  13.     {  
  14.         //else we are displaying an alert message  
  15.         alert('Enter a valid number');  
  16.     }  
  17. }  

Add the numbers.

The next article of this series explains the string function. Until then keep learning.

I hope you like it.

Thank you.

Up Next
    Ebook Download
    View all
    Learn
    View all