The concat() Function is used to join two Strings into one String.
Say, we have two Strings, Hello and World. And we want to join/concatenate them into one String, HelloWorld.
Let us solve it with the below example.
<html>
<body>
<script>
var x = "Hello"
var y = "World"
var z = x.concat(y)
document.write(z)
</script>
</body>
</html>
So, we have stored the String Hello in x.
var x = "Hello"
And, stored the String World in y.
var y = "World"
And used the + operator to join/concatenate Hello and World.
var z = x.concat(y)
And store the concatenated value in z.
-Function1.png)
But we cannot use the concat() function to join/concatenate a number and a String.
i.e. The below code will result into an error.
<html>
<body>
<script>
var x = 5
var y = "World"
var z = x.concat(y)
document.write(z)
</script>
</body>
</html>