最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery cookie sets boolean value as string - Stack Overflow

programmeradmin5浏览0评论

I'm working with a cookie, setting other code varibles using the value of the cookie.

I have defined data about the default state of the cookie as so:

const Cookie = {
  config: {
    name: 'Test_Cookie',
    expire: 1,
    value: true,
  },
...
}

When criteria are met, the cookie gets set for the first time, using this.config.value to set the value of the cookie to true:

  setCookie: function () {
    if (!this.isCookieSet()) {
      $.cookie(this.config.name, this.config.value, this.config.expire);
    }
  },

However, I am finding when I return the cookie value in the code I get "true" back as a string rather than just true. For example (name changed in above example for simplicity):

If I try to do a parison on the value of the cookie, and use === true I get a false result. If I do === "true" then I get a true result:

showStuff = $.cookie('Test_Cookie') === "true"; // showStuff = true;

OR

showStuff = $.cookie('Test_Cookie') === true; // showStuff = false;

Why does the variable type of the cookie value change when set?

I'm working with a cookie, setting other code varibles using the value of the cookie.

I have defined data about the default state of the cookie as so:

const Cookie = {
  config: {
    name: 'Test_Cookie',
    expire: 1,
    value: true,
  },
...
}

When criteria are met, the cookie gets set for the first time, using this.config.value to set the value of the cookie to true:

  setCookie: function () {
    if (!this.isCookieSet()) {
      $.cookie(this.config.name, this.config.value, this.config.expire);
    }
  },

However, I am finding when I return the cookie value in the code I get "true" back as a string rather than just true. For example (name changed in above example for simplicity):

If I try to do a parison on the value of the cookie, and use === true I get a false result. If I do === "true" then I get a true result:

showStuff = $.cookie('Test_Cookie') === "true"; // showStuff = true;

OR

showStuff = $.cookie('Test_Cookie') === true; // showStuff = false;

Why does the variable type of the cookie value change when set?

Share Improve this question asked Sep 6, 2016 at 12:51 user1486133user1486133 1,4873 gold badges22 silver badges44 bronze badges 2
  • 2 Cookies are really just strings. Saving a boolean as a cookie will in fact make it a string. The same will happen for any value you save as a cookie. – Wesley Smith Commented Sep 6, 2016 at 12:54
  • FYI, the same goes for localstorage – Wesley Smith Commented Sep 6, 2016 at 13:02
Add a ment  | 

2 Answers 2

Reset to default 4

Cookies are strings. You'll need to convert the cookie value to the type you want. The boolean values are being saved as true or false because that's the string representation of a boolean.

You can use the following.

var myBool = Boolean($.cookie('Test_Cookie'));

or

var myBool = ($.cookie('Test_Cookie') === "true");

EDIT As suggested in the first ment by @DelightedD0D:

You could also try - $.cookie('Test_Cookie') === "true"

For future readers: You can check against the string value as noted in other answers or convert it to a Boolean value for greater flexibility.

function stringToBoolean(string) {
  switch(string.toLowerCase()) {
    case "false": case "no": case "0": case "": return false;
    default: return true;
  }
}

const isTrue = stringToBoolean("true");
const isFalse = !isTrue;

ref: How can I convert a string to boolean in JavaScript?

发布评论

评论列表(0)

  1. 暂无评论