Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a variable, before I use that variable,I need to add string quotes to the value/data which is inside the variable using JavaScript need help

 //i am getting like this //
    var variable=king;

//i want to convert it with single quote//
     variable=variable;

but i need the data inside variable should be in a single quotation.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
179 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can concatenate the variable with your quotes like :

function addQuotes(value){
    var quotedVar = "'" + value + "'";
    return quotedVar;
}

And use it like :

var king = addQuotes('king');  

console.log will display :

'king'

Edit : you can try it in the chrome/firefox console, I did it and it works perfectly when copy/paste from here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...