JavaScript
JavaScript String Type
Concatenation
String concatenation, or the process of joining multiple strings, can be done in two ways where one is the syntax sugared version of the other.
We can concatenate as many strings as we want. JavaScript performs automatic type coercion, casting to target type, if an argument is not a string.
Modern JavaScript provides an addional way of formatting strings which we are going to look at in the formatting section.
const author = "Frank" + " Herbert"
This is equivalent to the code below.
const author = new String("Frank").concat(" Herbert")
Read more on MDN
String.prototype.concat()
Test yourself
Description
Define a new function with the name
dunify
that takes a string input parameter and prefixes the string with
Dune:
The prefix should be followed by a whitespace character and then the input parameter string.
Tests
- 1
You should define a function named
dunify. - 2
dunify("Fear is the mind-killer.")should return"Dune: Fear is the mind-killer.". - 3
dunify(1965)should return"Dune: 1965".