DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Updated on

Self-Taught Developer Journal, Day 24: TOP JavaScript Fundamentals Part 1 - Numbers cont. and Operators

Today I learned...

Infinity

Infinity (or -Infinity) is the value JavaScript will return if you calculate a number exceeding the largest possible number.

A division by 0 will also return Infinity.

Infinity is a number type.

Hexadecimals

JavaScript interprets numbers beginning with "0x" as a hexadecimal.

Never write a number with a leading zero (07) because some JavaScript versions interpret numbers as octal.

By default, JavaScript displays numbers as base 10 decimals. The toString() method can be used to output numbers from base 2 to base 36.

  • Hexadecimal - base 16
  • Decimal - base 10
  • Octal - base 8
  • Binary - base 2

Numbers as Objects

Numbers can also be defined as objects using the keyword new.

let o = new Number(100);
Enter fullscreen mode Exit fullscreen mode

However, its best not to create Number objects since it may produce unexpected results, complicates the code, and slows down the execution speed.

Operators

An operator is unary if it has a single operand.

let x = -1;
Enter fullscreen mode Exit fullscreen mode

When the unary plus operator, "+", is used on an operand that is not a number, it converts it into a number. An empty string and null become 0, true is 1, and false is 0. Space characters (\t, \n, etc.) are trimmed off when a string is converted to a number.

An operator is binary if it has two operands.

let x = 8 - 1;
Enter fullscreen mode Exit fullscreen mode




Resources

The Odin Project
https://www.w3schools.com/js/js_numbers.asp
https://javascript.info/operators

Discussion (0)