AppDividend
Latest Code Tutorials

JavaScript Math cosh(): Complete Guide

The cosh() is the static method of Math, you always use it as the Math.cosh(), rather than as a method of the Math object you created ( Math is not a constructor). The Math.cosh() method comes in handy in programming contexts dealing with any trigonometric expressions.

Since cosh() is the static method of Math, it can be used without creating an object.

JavaScript Math cosh()

The Math cosh() is a built-in JavaScript function that finds the hyperbolic cosine value of a given argument.  The Math.cosh() function returns a hyperbolic cosine of the number that can be expressed using the constant e

Syntax

Math.cosh(x)

Parameter(s)

The variable x, whose hyperbolic cosine value is to be determined.

Return Value

The hyperbolic cosine value.

Polyfill

Math.cosh = Math.cosh || function(x) {
      return (Math.exp(x) + Math.exp(-x))/2;
};

OR

Math.cosh = Math.cosh || function(x) {
      var y = Math.exp(x);
      return (y+1/y)/2;
};

See the following figure.

 

JavaScript Math cosh()

Note:

If the passed value is not a valid number, the method returns NaN.

Compatibility(Version and above):

  1. Google Chrome v38
  2. Firefox v25
  3. Edge v12
  4. Opera v25 
  5. Safari v8
  6. Android webview 
  7. Chrome for Android v38
  8. Edge Mobile v12
  9. Firefox for Android v25
  10. Opera for Android 
  11. Safari on iOS v8
  12. Samsung Internet
  13. Node.js v0.12

Non-compatible with: Internet Explorer 

JavaScript version: ECMAScript 6

example1.js:

See the following code example.

//example1.js

var a = 1;
var b = -1;
var c = 0;
var d = 4;
var e = -2;
var f = 2;


console.log(Math.cosh(a));
console.log(Math.cosh(b));
console.log(Math.cosh(c));
console.log(Math.cosh(d));
console.log(Math.cosh(e));
console.log(Math.cosh(f));

Output

node example1
1.5430806348152437
1.5430806348152437
1
27.308232836016487
3.7621956910836314
3.7621956910836314

example2.js

The following example demonstrates the case where values other than valid numbers are passed.

// example2.js

var a = "Hello, world";
var b;

console.log(Math.cosh(a));
console.log(Math.cosh(b));

Output

node example2
NaN
NaN

example3.js

The cosh() method cannot be used with complex arguments as only integer arguments are accepted.

// example3.js

// Complex values cannot be passed as arguments as follows
// since only integer arguments are accepted.

console.log(Math.cosh(2 + i));

Output

node example3
ReferenceError: i is not defined

example4.js

The following example demonstrates the use of polyfill for this method.

// example4.js

var a = 1;
var b = -1;
var c = 0;
var d = 4;
var e = -2;
var f = 2;

function polyfill1(x) {
  return (Math.exp(x) + Math.exp(-x)) / 2;
}

function polyfill2(x) {
  var y = Math.exp(x);
  return (y + 1 / y) / 2;
}

console.log(Math.cosh(a));
console.log(Math.cosh(b));
console.log(Math.cosh(c));
console.log(Math.cosh(d));
console.log(Math.cosh(e));
console.log(Math.cosh(f));
console.log();

console.log(polyfill1(a));
console.log(polyfill1(b));
console.log(polyfill1(c));
console.log(polyfill1(d));
console.log(polyfill1(e));
console.log(polyfill1(f));
console.log();

console.log(polyfill2(a));
console.log(polyfill2(b));
console.log(polyfill2(c));
console.log(polyfill2(d));
console.log(polyfill2(e));
console.log(polyfill2(f));

Output

node example4
1.5430806348152437
1.5430806348152437
1
27.308232836016487
3.7621956910836314
3.7621956910836314

1.5430806348152437
1.5430806348152437
1
27.308232836016487
3.7621956910836314
3.7621956910836314

1.5430806348152437
1.5430806348152437
1
27.308232836016487
3.762195691083631
3.7621956910836314

Conclusion

If you want to find the hyperbolic cosine value of a given number, then you can use the Javascript math.cosh() function.

See also

Javascript Math.abs()

Javascript Math.round()

Javascript Math.random()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.