-1

I'm sorry. I don't know how to describe this problem carefully.so I hope put my code first.

var test = 'less';
function test_I(tite) {
    console.log(tite);
}
var repeat = "test_I('"+test+"')";
setTimeout(repeat,10);

The code is right, but when I change:
var repeat = "test_I(' "+test+" ')";
to:
var repeat = "test_I("+test+")";,

I get this error in Chrom's console. Why do I need a ' ' When I pass a
String parameter? The error is below.

error

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
ice cold
  • 1
  • 1

2 Answers2

0

Why I need a ' ' When I pass a String parameter

To ensure that less is passed as a String instead of a variable.

If you don't use '', this code var repeat = "test_I("+test+")"; becomes

var repeat = "test_I(less)";

and when setTimeout executes the same it will look for variable less.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Ah, yes ... interesting fun. If you break down your string to what the JavaScript engine will see, it becomes:

test_I(less) 

and less doesn't exist.

if you added "var less='less'" at the beginning of your code, it would then work.

theGleep
  • 1,179
  • 8
  • 14