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.
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.
Share Improve this question edited Aug 25, 2016 at 13:39 Rafi Ud Daula Refat 2,25720 silver badges28 bronze badges asked Aug 25, 2016 at 12:25 kitty sarvajkitty sarvaj 5275 gold badges10 silver badges25 bronze badges 3-
1
How and from where do you retrieve the value
king
, Can you enclose it with''
there itself?\ – David R Commented Aug 25, 2016 at 12:29 - 1 assuming the variable contains string, you can concatenate it using quotes: var variable = "'"+king+"'"; (note the single quotes between doubles) – Bart K Commented Aug 25, 2016 at 12:29
- I am not sure what you are asking for . if you are not sure then parse it. variable.toString(). and if you want string quote you can do it. variable = '/''+ variable + '/''; – Rafi Ud Daula Refat Commented Aug 25, 2016 at 12:30
4 Answers
Reset to default 1You 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.
var x = 'abc';
var sData = "\'" + x +"\'";
// sData will print "'abc'"
var x = 'pqr'; var sData = "\'" + x +"\'";
// sData will print "'abc'"
1) You can use doublequotes
var variable = "'" + variable + "'";
2) ... or You can escape single quote symbol with backslash
var variable = '\'' + variable + '\'';