Skip to content Skip to sidebar Skip to footer

Adding The Single String Quote For Value Inside Variable Using Javascript

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 //

Solution 1:

You can concatenate the variable with your quotes like :

functionaddQuotes(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.

Solution 2:

Solution 3:

var x = 'pqr'; var sData = "\'" + x +"\'";

// sData will print "'abc'"

Solution 4:

1) You can use doublequotes

var variable = "'" + variable + "'";

2) ... or You can escape single quote symbol with backslash

var variable = '\'' + variable + '\'';

Post a Comment for "Adding The Single String Quote For Value Inside Variable Using Javascript"