JavaScript Basics

JavaScript Cheat Sheet — Numbers, Strings, and Regex

Spread the love

JavaScript is one of the most popular programming languages for web programming.

In this article, we’ll look at the basic syntax of modern JavaScript.


The toFixed method lets us round a number:

(3.14).toFixed(0);  // returns 3

The toPrecision method lets us round a number:

(3.14).`toPrecision`(1);  // returns 3.1

The valueOf method returns a number:


The Number function lets us convert anything to a number:


parseInt converts non-numeric values to an integer:

parseInt("3 months");

parseFloat converts non-numeric values to a floating-point number:

parseFloat("3.5 days");

The Number function also comes with some constant properties.

They include:

  • Number.MAX_VALUE — largest possible JS number
  • Number.MIN_VALUE — smallest possible JS number
  • Number.NEGATIVE_INFINITY —  negative infinity
  • Number.POSITIVE_INFINITY — positive infinity


We can do various mathematical operations with the Math object.

Math.round rounds a number to an integer:


Math.pow raises a base to an exponent:

Math.pow(2, 8)

Math.sqrt takes the square root of a number:


Math.abs takes the absolute value of a number:


Math.ceil takes the ceiling of a number:


Math.floor takes the floor of a number:


Math.sin takes the sine of a number:


Math.cos takes the cosine of a number:


Math.min returns the minimum number in the list:

Math.min(1, 2, 3)

Math.max returns the max number in the list:

Math.max(1, 2, 3)

Math.log takes the natural log of a number:


Math.exp raises e to the given power:


Math.random() generates a number between 0 and 1 randomly:


We can generate any random number by using Math.floor and Math.random together:

Math.floor(Math.random() * 5) + 1;

5 is the max number and 1 is the min.

Global Functions

We can use the String function to convert non-string values to strings:


We can also call toString on primitive values and objects to do the same:


The Number function lets us convert non-numbers to numbers:


decodeURI unescapes URLs:


encodeURI encodes URLs:


We can decode URI components with decodeURIComponent:


And we can encode a string into a URI string with encodeURIComponent :


isFinite lets us check whether a number is finite.

isNaN lets us check whether a value is NaN .

parseFloat lets us parse a value into a floating-point number.

parseInt lets us parse non-number values to integers.


JavaScript regex has the following modifiers:

  • i — perform case-insensitive matching
  • g — perform a global match
  • m — perform multiline matching

And they can have the following patterns:

  • “ — Escape character
  • d — find a digit
  • s — find a whitespace character
  • b — find a match at the beginning or end of a word
  • n+ — contains at least one n
  • n* — contains zero or more occurrences of n
  • n? — contains zero or one occurrence of n
  • ^ — start of string
  • $ — end of string
  • uxxxx — find the Unicode character
  • . — Any single character
  • (a|b) — a or b
  • (...) — Group section
  • [abc] — In range (a, b or c)
  • [0–9] — any of the digits between the brackets
  • [^abc] — Not in range
  • s — White space
  • a? — Zero or one of a
  • a* — Zero or more of a
  • a*? — Zero or more, ungreedy
  • a+ — One or more of a
  • a+? — One or more, ungreedy
  • a{2} — Exactly 2 of a
  • a{2,} — 2 or more of a
  • a{,5} — Up to 5 of a
  • a{2,5} — 2 to 5 of a
  • a{2,5}? — 2 to 5 of a, ungreedy
  • [:punct:] — Any punctu­ation symbol
  • [:space:] — Any space character
  • [:blank:] — Space or tab


JavaScript comes with many useful functions.

We can use regex to match patterns in strings.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *