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

The topics we 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. Different methods to convert string to number

How to concatenate two string value

To concatenate a string, we use “+” operator.

  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. //to concatenate a variable we use +
  11. alert(firstValue+secondValue);
  12. </script>
  13. </body>
  14. </html>

** JavaScript Alert **

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 above html, we have two variables, one contains a value of type string and other contains a value to type integer.

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

** age 33 **

Example 2

In this example we will prove if an integer value is converted into a string 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 gets concatenated.

** 2233 **

How to add two numbers

To add a number we use “+” operator.

  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>

** 5 **

How to subtract two numbers

To subtract two numbers we use “-” operator.

  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>

** 1 **

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 gets 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 gets converted to string, How will the number and string will be concatenated as we know to concatenate two variables or values we have to use “+” operator but here we are using “–“ 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 above html, we have two variables. One holds a value of type string and other holds a value of type integer.

In the alert function we are subtracting firstValue by secondValue.

Open file in browser.

** NaN **

We got the output as NaN.

This NaN states Not a Number. We will discuss about 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 file in browser.

** 2 **

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

NOTE

Whenever we use “+” to add/concat string or a numeric string to an integer, the integer value gets converted into a string whereas when we use “–“ 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 get converted into an integer. But when we subtract a numeric string value with an integer, the numeric string value gets converted into an integer and we get the result.

Methods to convert string into a number

The first thing we need to do is 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 file in browser.

** sum **

Now the next thing we need to do is create an External JavaScript file with a 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 document object and from that we can retrieve and assign the textbox value.

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

Pass the above JavaScript function to the button’s onClick event.

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

Pass the first and second number

** pass numbers **

Click the sum button.

** click sum **

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 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 string to an integer.

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

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

** add number **

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

** text passed **

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 isNaN function.

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

Add the numbers


** valid **

** valid1 **

In the next article of this series we will learn about string function. Until then keep learning.

I hope you like it.

Thank you