Feeds:
Posts
Comments

Archive for January 10th, 2011

Greetings

Today i learnt how to concatenate a string in different ways in Javascript

It is done using the following methods

a. The addition operator ( + )

b. The assignment operator ( += )

c. The built-in concat method

Example for each method is given below

Using addition operator ( + ) :


<html>
<body>
<script type="text/javascript">
 var str1 = "Welcome";
 var str2 = "To";
 var str3 = "Javascript";
 var result = str1 +" "+ str2 +" "+ str3;
 document.write(result);
</script>
</body>
</html>

Using the assignment operator (+=)


<html>
<body>
<script type = "text/javascript">
 var str1 = "Welcome";
 str1 += " To";
 document.write(str1);
 </script>
</body>
</html>

Using the ‘  concat ‘ method


<html>
<body>
<script type="text/javascript">
 var str1 = "".concat("Welcome ","To ","Javascript");
 document.write(str1);
</script>
</body>
</html>

However multiple ways exists, the addition operator ( + ) is widely used one

Advertisements

Read Full Post »