2

I'm new in react native .I want to append dynamic two strings into one common string how can i do this?

Please fine below code

var commonHtml = 'my name is %s from %s';
var a ='ravi';
var b = 'chennai';

expected output:

'my name is ravi from chennai'

1
  • try this var commonHtml = `my name is {a} is from {b}`; Commented Apr 17, 2017 at 9:44

1 Answer 1

18

There is this feature called template literals in javascript that allows you to do that. So your code would be like this:

var a ='ravi';
var b = 'chennai';
var commonHtml = `my name is ${a} from ${b}`;

console.log(commonHtml); // output: 'my name is ravi from chennai'

EDIT: Changed the erroneous variable name

Sign up to request clarification or add additional context in comments.

2 Comments

Running the code snippet through an error. But it's a correct answer ( just don't forget to use ` instead of ' )
PS: try not to use inappropriate variable names,though i have up vote cause of correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.