Wondering if this is possible and if so , how ?
var variable = true;
if (variable) {
var newVariable = "$('table').find('tr').length > 1)";
} else {
var newVariable = "$('table').find('tr').length > 4)";
}
if (newVariable){ // if $('table').find('tr').length > 1)
console.log(newVariable)
}
I would like the if to look like this by inserting using the newVariable when variable is true
if ($('table').find('tr').length > 1)){
console.log(newVariable)
}
of if variable is false then this
if ($('table').find('tr').length > 4)){
console.log(newVariable)
}
2
Answers
newVariable
is a string. Using it in anif
condition is not going to execute it, it will just test if the string is empty or not.You can use a function instead, and call it in the
if
.Unclear why you would need to use strings. Only way you could do that is use eval or new Function and that is not a good idea. Seems like the code could be cleaned up to be either
better yet reducing the code with ternary
Or better yet using ternary