JavaScript: The Ultimate Guide to Understand JavaScript Code and its Fundamentals. Discover Literal and Control Flow. Learn Variables, Functions, Object and the Best jQuery. by Mark Graph

JavaScript: The Ultimate Guide to Understand JavaScript Code and its Fundamentals. Discover Literal and Control Flow. Learn Variables, Functions, Object and the Best jQuery. by Mark Graph

Author:Mark Graph [Graph, Mark]
Language: eng
Format: azw3
Published: 2020-01-30T16:00:00+00:00


The String Concatenation Operator

The + operator can also be used to combine two strings into a single string:

var s = prompt ("Enter your name.", "");

alert ("Your name is " + s + ".") ;

One common use for string concatenation is to convert a non-string value to a string and combine it with another string, all in a single operation:

var count = 0;

/* code that increments count... */

alert ("Final count: " + count);

If only one of its operands is a string, + converts the other operand to a string and joins the two strings. (The + operator favors a string over a number. If one operand is a string and the other a number, + does not attempt to convert the string to a numeric value and add the two numbers!)

Recall from hat the automatic conversion of a number, boolean value, null, or undefined to a string works as expected and is one of the benign forms of automatic conversion. An easy way to make this conversion is by using the + operator. If you don't want to add any characters, you can just concatenate with an empty string:

var count = 0;

/* code that increments count... * /

/* done incrementing 'count'; now convert it to a string: */

var s = count + "";



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.