javascript - Checking if token is available - Stack Overflow

Code that does not work:console.clear();try {test(token);}catch(err) {console.log(err.toString());}i

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 badges
Add a ment  | 

1 Answer 1

Reset to default 9

Use:

if(typeof token === 'undefined')
  • typeof is a keyword, not a function
  • typeof always returns a string

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744958555a4603345.html

相关推荐

  • javascript - Checking if token is available - Stack Overflow

    Code that does not work:console.clear();try {test(token);}catch(err) {console.log(err.toString());}i

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信