String Number Operations in JavaScript

In this article you learn the basics of JavaScript and create your first JavaScript program.

String Number Operations in JavaScript

String Number Operations in JavaScript

     

In this article you learn the basics of JavaScript and create your first JavaScript program.

 What is String Number Operation?
The JavaScript  String is a loosely type language. This is not mean that it has no data types that the value of a variable or a JavaScript object property does not need to have a particular type of the value assigned, or that it should be always hold the same type of value. JavaScript also freely type-converts values into a type require by the context of their use. JavaScript being is the loosely typed and willing to type-convert still does not save the programmer from need to the think about the actual type of values that they are dealing with. A very common error in browser  scripting. for example, it is to read the value property of a form control into which the user is expected to type a number and then add that value to another number. Because the value properties of form controls are strings if the even then character is sequence they contain represents a number of the attempt to add that string to a value, even if the value happens to be a number, results in the second value being type-converted into a string and concatenated to the end of the first string value from the from control. The problem arises from dual nature of the + operator used for the both numeric addition and string concatenation. which the nature of the operation performed is determined by the context, where only both the operation are numbers to start with the + operator perform addition. Otherwise it converts all of its operands to strings and does concatenation. 

Converting String:
The String most often from the action + Operators. when one of its operators not a number. The easy way of getting the string that results from type-conversion is to concatenate a value to string. The alternative method of convert a value into a string to pass it is argument to the String constructor called as a function. Exam:-

   Var string Value= String(d);

Converting Number:
The Converting values to numbers especially to strings numbers, it is an extremely common requirement and many methods can be used. There are any mathematical operation except the concatenation addition operator will force type-conversion. So the conversion of a string to a number might entail performing a mathematical operation on the string representation of the number that would not affect the resulting number, such as subtracting zero or multiplying by one.

var numValue = stringValue - 0; 
var numValue = stringValue * 1;
var numValue = stringValue / 1;

String in JavaScript
The JavaScript language until now we have been focusing on the language constructs of JavaScript: if statements, loops, functions, etc. This primer are going to take a step back and cover the inner workings of some of the native JavaScript objects: Strings, Numbers and Arrays. 

Example:

<html>
<head>
<Script language="JavaScript">
document.write(' Test'.indexOf('T')); 
document.write('This '.lastIndexOf('T')); 
document.write('This is test'.charAt(10)); 
document.write('This program Test'.length); 
document.write(' program'.substring(1, 10)); 
document.write('my exam Test'.substr(7, 10)); 
document.write('my program'.toUpperCase()); 
document.write(' first JavaScript program'.toLowerCase()); 
document.write("<br />")
</script>
</table>
</body>
</html>

 

Array in JavaScript
Array is the basically just a list of items. Each item an array can be whatever you want, but they are usually related to one-another.

Example:

 <html>
<head>
<script language="JavaScript">
var students = ['amar', 'rani', 'vinod', 'susil','santosh'];
var suffixes = ['1st', '2nd', '3rd', '4th','5th'];
for(var i=0; i<=4; i++)

alert('The '+suffixes[i]+' student is '+students[i]);
}
</script>
</head>
</body>
</html>

JavaScript in Number Operation
The numbers is a central task for the many programs. In fact, the desire to automate number operations was the motivation that drove the invention of digital computers decades ago. we will learn how to work with numbers in Java, including arithmetic, conversions, and other numeric operations. 

Example:
  

<html>
<head>
<Script language="JavaScript">
var sum = 10 + 10;
sub = 15 - 5;
mul = 25 * 5; //multiplication
divided = 30 / 5;
modulus = 10 % 10; //modulus division
document.write("sum:"+sum); 
document.write("sub:"+sub);
document.write("mul:"+mul); 
document.write("divided:"+divided);
document.write("modulus:"+modulus); 
</script>
</head>
</body>
</html>