Search⌘ K
AI Features

Playing Around With Strings

Explore string operations in JavaScript including concatenation and immutability. Understand automatic type casting and master converting strings to integers and floats using parseInt and parseFloat methods for practical coding tasks.

Concatenating strings

A string is basically a series of characters. Let’s perform some operations on strings.

Node.js
console.log('Javascript in ' + 'Practice')

The + concatenates strings. Concatenation means that you join the contents of two strings one after the other.

Strings are immutable, which means that their value cannot be changed. When concatenating two strings, the result is saved in a third-string.

Automatic type casting

If any of the operands of plus is a string, the result becomes a string. JavaScript automatically converts the operands of an operator to the same type. This is called automatic type casting.

Node.js
console.log("1 + \'2\' becomes",1 + '2');
console.log("\'1\' + 2 becomes",'1' + 2);

Rules may become confusing, so don’t abuse automatic typecasting.

Parsing strings as integers

You may have to explicitly cast a string to an integer to be able to add it to another integer:

Node.js
console.log("1 + + \"2\"")
console.log(1 + +"2") // +"2" gives a sign to "2", converting it to a number
console.log("1 + Number(\"2\")")
console.log(1 + Number("2"))
console.log("1 + Number.parseInt( \"2\", 10 )")
console.log(1 + Number.parseInt( "2", 10 ))
console.log("1 + Number.parseInt( \"2\" )")
console.log(1 + Number.parseInt( "2" ))

All the above conversions work. The first relies on giving a sign to a numeric string which converts it to a number. Then 1+2 becomes 3. The second typecast is more explicit: you use Number to wrap a string and convert it to a number.

I recommend using the third option: Number.parseInt with a radix. parseInt converts a string into a number. The second argument of parseInt is optional: it describes the base in which we represent the number.

Node.js
console.log("Number.parseInt(\"JS in Practice\")");
console.log(Number.parseInt("JS in Practice"));
console.log("Number.parseInt( \"10\", 2 )");
console.log(Number.parseInt( "10", 2 ));
console.log("Number.parseInt( \"a\" )");
console.log(Number.parseInt( "a" ));
console.log("Number.parseInt( \"a\", 16 )");
console.log(Number.parseInt( "a", 16 ));

Arbitrary strings are often NaN. “2” in base 2 is 10. You can see how easy it is to convert a binary or a hexadecimal (base 16) string into a decimal number. Base 16 digits are 0123456789abcdef. The last 6 digits may also be upper case.

Number.parseInt recognizes the starting characters of a string as integer numbers and throws away the rest:

Node.js
console.log(Number.parseInt( "1234.567 89" ));

Parsing Strings as floating numbers

The dot is not a character present in integer numbers, so everything after 1234 is thrown away by Number.parseInt.

You can also use Number.parseFloat to parse floating point. It parses the floating-point number until the terminating space:

Node.js
console.log(Number.parseFloat( "1234.567 89" ));
Technical Quiz
1.

What will be the output of

console.log(Number.parseInt( "f", 16 ));
A.

10

B.

NaN

C.

15


1 / 1