Code that does not work: console.clear();
try {
test(token);
}
catch(err) {
console.log(err.toString());
}
if(typeof token === undefined) {
console.log("token exists");
}
test(token);
if(token) {
console.log("token exists");
}
function test(o) {
if(o) {
console.log("token exists");
}
}
JSBin: ,js,console,output
Question: How can I test a 'token' for existence where it doesn't throw a reference error if it doesn't?
All three of my examples throw a ReferenceError: token is not defined
Code that does not work: console.clear();
try {
test(token);
}
catch(err) {
console.log(err.toString());
}
if(typeof token === undefined) {
console.log("token exists");
}
test(token);
if(token) {
console.log("token exists");
}
function test(o) {
if(o) {
console.log("token exists");
}
}
JSBin: http://jsbin./qanuk/1/edit?html,js,console,output
Question: How can I test a 'token' for existence where it doesn't throw a reference error if it doesn't?
All three of my examples throw a ReferenceError: token is not defined
Share Improve this question asked May 28, 2014 at 19:40 Mike CheelMike Cheel 13.1k10 gold badges77 silver badges105 bronze badges1 Answer
Reset to default 9Use:
if(typeof token === 'undefined')
typeof
is a keyword, not a functiontypeof
always returns a string
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744958555a4603345.html
评论列表(0条)