Categories
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.

Numbers

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:

(3.14).valueOf();

The Number function lets us convert anything to a number:

Number(true);

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

Math

We can do various mathematical operations with the Math object.

Math.round rounds a number to an integer:

Math.round(4.1);

Math.pow raises a base to an exponent:

Math.pow(2, 8)

Math.sqrt takes the square root of a number:

Math.sqrt(49);

Math.abs takes the absolute value of a number:

Math.abs(-3.14);

Math.ceil takes the ceiling of a number:

Math.ceil(3.14);

Math.floor takes the floor of a number:

Math.floor(3.14);

Math.sin takes the sine of a number:

Math.sin(0);

Math.cos takes the cosine of a number:

Math.cos(0);

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.log(1);

Math.exp raises e to the given power:

Math.exp(1);

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

Math.random();

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:

String(23);

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

(23).toString();

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

Number("23");

decodeURI unescapes URLs:

decodeURI(enc);

encodeURI encodes URLs:

encodeURI(uri);

We can decode URI components with decodeURIComponent:

decodeURIComponent(enc);

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

encodeURIComponent(uri);

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.

Regex

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

Conclusion

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 *