Monday, October 4, 2010 | By: mayurJavascript

Operator In Javascript

An operator in JavaScript is said to be any symbols or group of symbols that performs a very specific function, usually on numbers. Operators are used extensively in JavaScript because of there flexibility - they can be applied not only to numbers, but in special cases can also be applied to strings and other data types. Many of the mathematical functions of operators bring data type conversion into play. The rules for data type conversion are given here and in the JavaScript Language Reference, Operators. There are seven general types of operators, as follows.
  • Arithmetic Operators
  • The String Operator
  • Assignment Operators
  • Logical Operators
  • Comparison Operators
  • The Conditional Operator
  • Bitwise Operators
Each has its own set of rules governing its use and functionality. Each general class has several different operators that you must have an understanding of in order to move on to the higher functions such as loops and conditional statements. Each general type of operator is described in detail in the coming sections. Each operator is also defined in depth and with working examples in the JavaScript Language Reference, Operators.

Arithmetic Operators

An arithmetic operator obviously works with numbers and is used within arithmetic operations. There is a wide variety of operators to work with, from the simple to the advanced. There are many built in mathematical functions to the JavaScript language that are covered in the Numbers Data Type section. Since we've already looked at them, we'll not include them here. There are, however, many different functions available through the Math object. These mathematical methods are listed below.
  • Math.abs() - This method is used to calculate the absolute value of a number.
  • Math.acos() - This method is used to calculate the arc- cosine of a number.
  • Math.asin() - This method is used to calculate the arc-sine of a number.
  • Math.atan() - This method is used to calculate the arc- tangent of a number.
  • Math.atan2() - This method is used to calculate the arctangent of the quotient of its given parameters.
  • Math.ceil() - This method is used to calculate the ceiling of the numbers being worked with.
  • Math.cos() - This method is used to calculate the cosine of a number.
  • Math.exp() - This method is used to calculate the natural exponent of a number.
  • Math.floor() - This method is used to return the floor value of the numbers being worked with.
  • Math.log() - This method is used to calculate the natural logarithm of a number.
  • Math.max() - This method is used to calculate the maximum value for the two parameters passed.
  • Math.min() - This method is used to calculate the minimum value for the two parameters passed.
  • Math.pow() - This method is used to calculate the power of the number passed as the parameter.
  • Math.random() - This method is used to calculate a random number.
  • Math.round() - This method is used to round the number given to the nearest whole number.
  • Math.sin() - This method is used to calculate the sine of a number.
  • Math.sqrt() - This method is used to calculate the square root of a number.
  • Math.tan() - This method is used to calculate the tangent of a number.
Each of the above methods, used with the Math object, give a wide array of functionality to your scripting actions. They may be used alone or in combinations. All of the common arithmetic operators will attempt to convert strings to numbers. If this is not possible, the NaN (Not A Number) data type will be returned. While the above listing is a list of mathematical methods, the following is a list of arithmetic operators, which are different from methods. Each is explored in detail. Again, all of the following are available in an in depth form in the JavaScript Language Reference, Operators.
  • Addition ( + ) - The addition operator is the most commonly used operator. It is obviously used to add two numbers together. When the values on either side of the addition operator are numbers, they are added together. When they are strings, they are concatenated, the second being appended to the end of the first. If a number and a string are being added, the string is converted to a number, then added to the other number. If this conversion is not possible, NaN is returned.
  • Subtraction ( - ) - The subtraction operator is used in subtraction. The subtraction operator subtracts the right number from the left. If either or both of the values are strings, an attempt is made to convert them to numbers. If this is not possible, the value of NaN is returned.
  • Multiplication( * ) - The multiplication operator is used to multiply the left value by the right. When the values on either side of the multiplication operator are numbers, they are multiplied together. When they are strings, they are converted to numbers. If a number and a string are being added, the string is converted to a number, then multiplied by the other number. If this conversion is not possible, NaN is returned.
  • Division ( / ) - The division operator is used to divide the left value by the right value. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Modulus ( % ) - The modulus operator is an interesting one. It is used to return the remainder of the division of the two values given. Neat. As with the division operator, the left value is divided by the right. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Pre-Increment ( ++value ) - The pre-increment operator is a very useful and time saving operator. It is used to increase the number given by one, then return that new, incremented number. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Post-Increment ( value++ ) - The post-increment operator is used to increase the given number by one, but even though the new, decremented number is saved within the variable, it is not returned - the original number is. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Pre-Decrement ( --value ) - The pre-decrement operator is used to decrease the number given by one then return that new, decremented number. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Post-Decrement ( value-- ) - The post-decrement operator is used to decrease the given number by one, but even though the new, decremented number is saved within the variable, it is not returned - the original number is. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.
  • Unary Negation ( - ) - The unary negation operator is used to change the polarity of a number. That is, it converts a positive number to a negative number. You might have noticed that the Unary Negation operator is the same as the subtraction operator. Only its use differs. Use it in front of a positive number to change it to a negative number. Use it in front of a negative number to change it to a positive number. If either or both of the values are a string, an attempt is made to convert the string to a number. If this is not possible, NaN is returned.

0 comments:

Post a Comment