Sign of Number in JavaScript

Level 128 keystrokes

Math.sign() tells whether a number is negative, zero or positive; for -42 it prints -1.

JavaScript code

// Math.sign() returns -1 for negatives, 1 for positives and 0 for zero
// Only the sign matters, not the size, so -42 gives -1
1console.log(Math.sign(-42));

Output

-1
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • .× 2
  • (× 2
  • )× 2
  • -× 1
  • ;× 1

More JavaScript examples at level 1

← JavaScript typing practice