Object-Oriented JavaScript — String and Math

John Au-Yeung - Jan 24 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

JavaScript is partly an object-oriented language.

To learn JavaScript, we got to learn the object-oriented parts of JavaScript.

In this article, we’ll look at the String and Math objects.

String Object Methods

Strings have various methods we can call to do stuff with.

The toUpperCase method lets us return the upper case version of the string.

For instance, if we have:

const s = 'foo';
Enter fullscreen mode Exit fullscreen mode

Then:

s.toUpperCase();
Enter fullscreen mode Exit fullscreen mode

returns 'FOO' .

The toLowerCase method converts a string to all lower case.

For example:

s.toLowerCase()
Enter fullscreen mode Exit fullscreen mode

returns 'foo' .

The charAt method returns the character found at the position we specify.

For instance, if we have:

s.charAt(0);
Enter fullscreen mode Exit fullscreen mode

then we get 'f' .

We can just write s[0] for short.

If we pass in a non-existent position to charAt , we get an empty string.

The indexOf method lets us search for a substring within a string.

For instance, if we have:

s.indexOf('o');
Enter fullscreen mode Exit fullscreen mode

we get 1.

Also, we can optionally specify the position to start the search with indexOf .

For instance, we can write:

s.indexOf('o', 2);
Enter fullscreen mode Exit fullscreen mode

and we get 2.

The lastIndexOf method starts the search from the end of the string.

For instance, if we write:

s.lastIndexOf('o');
Enter fullscreen mode Exit fullscreen mode

we get 2.

We can also search for a series of characters.

For example, if we have:

const str = 'foobar';
str.indexOf('bar');
Enter fullscreen mode Exit fullscreen mode

we get 3.

They can be combined together.

So we can write:

s.toLowerCase().indexOf('foo'.toLowerCase());
Enter fullscreen mode Exit fullscreen mode

The slice() and substring() methods return a piece of the string when we specify the start and end position.

If we have:

const str = 'foobar';
str.slice(1, 3);
str.substring(1, 3);
Enter fullscreen mode Exit fullscreen mode

and we get 'oo' .

The split method lets us split a string by a separator.

For instance, we can write:

const s = 'foo bar';
const arr = s.split(" ");

console.log(arr);
Enter fullscreen mode Exit fullscreen mode

Then arr is [“foo”, “bar”] since we split by a space.

The join method joins an array of strings into one with a separator between them.

So if we have:

["foo", "bar"].join(' ')
Enter fullscreen mode Exit fullscreen mode

We get:

"foo bar"
Enter fullscreen mode Exit fullscreen mode

The concatr method appends a string into an existing string.

So if we have:

'foo'.concat("bar")
Enter fullscreen mode Exit fullscreen mode

We get:

"foobar"
Enter fullscreen mode Exit fullscreen mode

Math

The Math object isn’t a function.

It’s an object that has various constants and methods.

Constants that it includes are:

  • Math.PI — pi
  • Math.SQRT2 — the square root of 2
  • Math.E — Euler’s constant
  • Math.LN2 — natural log of 2
  • Math.LN10 — nature log of 10

We can generate a random number of between 0 and 1 with Math.random() .

We can use the expression:

((max - min) * Math.random()) + min
Enter fullscreen mode Exit fullscreen mode

to generate a random number between min and max .

Math has various methods to round numbers.

floor() rounds down.

ceil() rounds up.

And round() rounds to the nearest integer.

We can use them by passing in a number:

Math.floor(9.1)
Math.ceil(9.1)
Math.`round`(9.1)
Enter fullscreen mode Exit fullscreen mode

Math.pow raises a base to an exponent.

For instance, kf we have:

Math.pow(2, 3);
Enter fullscreen mode Exit fullscreen mode

then we get 8. 2 is the base and 3 is the exponent.

The Math.sqrt method returns the square root of a number.

For instance, we can write:

Math.sqrt(9);
Enter fullscreen mode Exit fullscreen mode

and we get 3.

Conclusion

Strings have various methods to let us manipulate them.

The Math object lets us do various operations and provides us with some constants.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .