DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Edited on

3

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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more