为什么我的简单if语句在javascript中不显示为false? [英] Why doesn't my simple if-statement render false in javascript?

查看:56
本文介绍了为什么我的简单if语句在javascript中不显示为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样调用一个函数:

call_facebook(103138046395999, "samåkning.se", "http://www.facebook.com/samakning", "page");
call_facebook(16089699074, "Jag ska köra bil, vem vill åka med", "http://www.facebook.com/groups/16089699074/", "grupp")

函数如下:

function call_facebook(id, name, link, type){
  //snip
  console.log(type)
  if (type = "grupp"){
    var postid=fbposts[fbpost].id.split("_");
    return "https://www.facebook.com/groups/"+postid[0]+"/permalink/"+postid[1]+'/'
  }
  else {
    return fbposts[fbpost].actions[0].link;
  }
}
//snip
};

我已经确认它们具有不同的类型参数,但第一种情况 if(type ="grupp")始终以 true 结尾.为什么?

I have confirmed that they have different type arguments, but still the first case if (type = "grupp") always ends up true. Why?

推荐答案

单个等号字符( = )不进行比较.它分配!因此,您的 if 语句始终将"grupp" 分配为 type ,并返回 true .

A single equals character (=) does not compare. It assigns! Therefore, your if statement always assigns "grupp" into type and returns true.

使用 == === 进行比较.

这篇关于为什么我的简单if语句在javascript中不显示为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆