Categories
JavaScript Basics

Highlights of JavaScript — Find Characters and Convert Between Numbers and Strings

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Finding a Character at a Location in a String

We can find a character at a location in a string with the charAt method or the square brackets notation.

For example, we can write:

let firstChar = 'james'.charAt(0)

then firstChar is 'j' .

This is the same as:

let firstChar = 'james'[0]

They’re the same, so the square brackets are used more often since it’s shorter.

Replacing Characters

The replace method lets us replace characters.

For example, we can write:

let newText = text.replace("World War II", "the Second World War");

to replace “World War II” with the “the Second World War” .

It’ll search for the first instance of it and replace it.

To search all instances of it and replace all of them, we need to use a regex:

let newText = text.replace(/World War II/g, "the Second World War");

The g flag stands for global. This means it’ll replace all of them.

Rounding Numbers

We can use the Math.round method to round numbers.

For example, we can write:

let score = Math.round(11.1);

Then we round 11.1 to 11 with Math.round .

It rounds to the nearest integer.

The function rounds up when the decimal is .5.

To always round up to the nearest integer, we can use the Math.ceil method:

let score = Math.ceil(11.1);

Then we get 12.

To always round down to the nearest integer, we can use the Math.floor method:

let score = Math.floor(11.1);

Generating Random Numbers

JavaScript comes with a function to generate random numbers.

The Math.random method lets us create a random number between 0 and 1.

For example, we can write:

let randomNumber = Math.random();

The returned number has 16 decimal places.

Converting Strings to Integers and Decimals

Convert strings with numbers to an integer or decimal number is something that we often have to do.

We can use the parseInt method to convert a string into an integer.

And we can use the parseFloat method to convert a string into a decimal number.

For example, we can write:

let currentAge = prompt("Enter your age.");
let age = parseInt(currentAge);

We have the parseInt method to convert the entered number into an integer.

prompt always returns a string, so we’ll have to convert it to an integer.

To convert the entered text into a decimal number, we can use parseFloat :

let currentAge = prompt("Enter your age.");
let age = parseFloat(currentAge);

Converting Strings to Numbers and Vice Versa

To convert a string to a number, we can use the Number global function:

let num = Number('123');

Then num is 123 .

To convert a number to a string, we can use the toString method:

let num = 1234;
let numberAsString = num.toString();

The numberAsString is '1234' .

Conclusion

We can find and replace strings within a string.

Also, we can convert between numbers and strings and generate numbers with JavaScript.

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 *