1.

Role of “use strict” in JavaScript

Answer»

The Math.ABS() method is used in JavaScript to FIND the absolute value of a number.

The FOLLOWING is the parameter of abs(val) method:

  • val: The number for which you WANT the absolute value:

The method returns:

  • A Number representing the absolute value of the specified number, or
  • NaN, if the value is not a number, or
  • 0 if the value is NULL

Let us see an example to get the absolute value of a negative number:

var val = -9.50;

To get the absolute value:

Math.abs(val);

The entire example:

<!DOCTYPE html> <html> <body> <script>   var val = -9.50;   document.write("Number: "+val);   var res = Math.abs(val);   document.write("<br>Absolute Value: "+res); </script> </body> </html>

The output returns the negative value:

Number: -9.5 Absolute Value: 9.5

Let us see another example for “null” as input:

<!DOCTYPE html> <html> <body> <script>   var val = null;   document.write("Number: "+val);   var res = Math.abs(val);   document.write("<br>Absolute Value: "+res); </script> </body> </html>

The output:

Number: null Absolute Value: 0

Let us see another example to get the absolute value:

<!DOCTYPE html> <html> <body> <script>   var val = 15+30+55;   document.write("Number: "+val);   var res = Math.abs(val);   document.write("<br>Absolute Value: "+res); </script> </body> </html>

The output:

Number: 100 Absolute Value: 100


Discussion

No Comment Found