Learn JavaScript
  • Introduction
  • Basics
    • Comments
    • Variables
    • Types
    • Equality
  • Numbers
    • Math
    • Basic Operators
    • Advanced Operators
  • Strings
    • Creation
    • Replace
    • Length
    • Concatenation
  • Conditional Logic
    • If
    • Else
    • Switch
    • Comparators
    • Concatenate
  • Arrays
    • Unshift
    • Map
    • Spread
    • Shift
    • Pop
    • Join
    • Length
    • Push
    • For Each
    • Sort
    • Indices
  • Loops
    • For
    • While
    • Do...While
  • Functions
    • Higher Order Functions
  • Objects
    • Properties
    • Mutable
    • Reference
    • Prototype
    • Delete
    • Enumeration
    • Global footprint
  • Linked List
    • Add
    • Pop
    • Prepend
    • Shift
  • Browser Object Model (BOM)
    • Window
    • Popup
    • Screen
    • Navigator
    • Cookies
    • History
    • Location
  • Date and Time
  • JSON
  • Error Handling
    • try...catch...finally
  • Events
  • Regular Expression
  • Modules
  • Debugging
  • Classes
    • Static
    • Inheritance
    • Access Modifiers
  • Promise, async/await
    • Async/Await
  • Miscellaneous
    • Hoisting
    • Currying
    • Polyfills and Transpilers
  • Exercises
    • Console
    • Multiplication
    • User Input Variables
    • Constants
    • Concatenation
    • Functions
    • Conditional Statements
    • Objects
    • FizzBuzz Problem
    • Get the Titles!
Powered by GitBook
On this page

Was this helpful?

  1. Numbers

Basic Operators

Mathematic operations to numbers can be performed using some basic operators like:

  • Addition: c = a + b

  • Subtraction: c = a - b

  • Multiplication: c = a * b

  • Division: c = a / b

The JavaScript interpreter works from left to right. One can use parentheses just like in math to separate and group expressions: c = (a / b) + d

JavaScript uses the + operator for both addition and concatenation. Numbers are added whereas strings are concatenated.

NaN is a reserved word indicating that a number is not a legal number, this arises when we perform arithmetic with a non-numeric string will result in NaN (Not a Number).

let x = 100 / "10";

The parseInt method parses a value as a string and returns the first integer.

parseInt("10"); // 10
parseInt("10.00"); // 10
parseInt("10.33"); // 10
parseInt("34 45 66"); // 34
parseInt(" 60 "); // 60
parseInt("40 years"); //40 
parseInt("He was 40"); //NaN

In JavaScript, if we calculate a number outside the largest possible number it returns Infinity .

let x =  2 / 0; // Infinity
let y = -2 / 0; // -Infinity
PreviousMathNextAdvanced Operators

Last updated 2 years ago

Was this helpful?