This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I've read numerous examples converting from String to Boolean.
For example,
myString = (myString == "true");
But I'm unable to apply the logic to my code. Can someone help?
CommonConfig.getFeatureFlags['analytics.demo'] returns "true" (Note
the "").(That's how backend returns it)
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: String == CommonConfig.getFeatureFlags['analytics.demo'], //either "true" or "false" but I want to make it to true or false
});
Question: I want to convert from String "true" to boolean. And pass true or false based upon!
Can someone help?
Isn't it:
var FeatureFlag = _.extend(CommonConfig, {
demoFeature: "true" == CommonConfig.getFeatureFlags['analytics.demo'],
});
Related
This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I vaguely remember that I've seen custom values for a boolean true/false sometimes, but I have no idea what it is called or how exactly it was written.
So I have this code:
preferenceHotel = document.getElementById('preference-hotel').checked;
Now instead of preferenceHotel returning true or false, I would like it to return other values (yes or no), without writing an if statement checking whether it is true or false.
I remember something like this:
preferenceHotel = document.getElementById('preference-hotel').checked:"yes"|"no";
Does anybody know what I mean and know the name of it so I can read the documentation? (+ write it for my use case).
Thanks in advance!
You can do it by using Ternary operators in JavaScript
let preferenceHotel = (document.getElementById('preference-hotel').checked) ? "yes" : "no";
Conditional Operators
How about this:
preferenceHotel = document.getElementById('preference-hotel').checked ? "yes" : "no";
try:
preferenceHotel = document.getElementById('preference-hotel').checked?"yes": "no";
This question already has answers here:
How to check whether a Storage item is set?
(17 answers)
Closed 2 years ago.
I have a localStorage value, which I am getting
const marks = JSON.parse(localstorage.getItem('mark'))
Now I am trying to make it robust so that if there is no key with mark in localStorage then it should not break.
localstorage.getItem('mark') ? JSON.parse(localstorage.getItem('mark')) : []
So is there any another way through which I can do the null check ?
try:
localstorage.getItem('mark') === null ? [] : JSON.parse(localstorage.getItem('mark'))
EDIT
Of course, It means:
if(localstorage.getItem('mark') === null){
return '[]'
}else {
JSON.parse(localstorage.getItem('mark'))
}
If item named mark is not null it will parse to JSON and u will get the result
This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 6 years ago.
I read out a data-attibute, then I want to convert the string "true" into a boolean. At the moment I have to do a comparison in the javascript, is there a better way to do it? I don't know how to use this solution
HTML
<div data-nav='{ "nav": "true"}'>
JS
var data = JSON.parse($nav.attr('data-nav').toString());
data.nav = (data.nav === "true") ? true : false;
Try
<div id='test' data-nav='true'>
var truefalse = $('#test').data('nav');
.data should be able to evaluate it as boolean
here is an example in JSFiddle
https://jsfiddle.net/lismore/hx3gLvgw/
This question already has answers here:
How do you check if a selector matches something in jQuery? [duplicate]
(11 answers)
Closed 9 years ago.
In the jQuery accordion API, it says "If the accordion is collapsing, ui.newHeader and ui.newPanel will be empty jQuery objects."
How can I check if ui.newheader is an empty jQuery object? I've tried it like this:
if ($(ui.newHeader) == null)
{
...
}
,like this:
if (ui.newHeader == null)
{
...
}
and this:
if ($(ui.newHeader) == "")
{
...
}
So basically, this is a question about jquery/javascript syntax :) Thanks
What you want is to know if there is 0 element in the set. Do it like this :
if ($(ui.newHeader).length==0) {
if (!$(ui.newHeader).length)
or
if (!$(ui.newHeader)[0])
jQuery object is array like collection. So, it is empty means, it's length property is 0.
if(!$(ui.newHeader).length) {...}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javascript compare strings without being case sensitive.
I am having two fields. I need to check whether both the fields contain the same value. If the same value then a alert is raised.
I am using validation engine jQuery.
I have a written a function like:
function kkkk(field, rules,i,options){
var password=field.val();
var username = getValueUsingElementID('username');
if(password==username){
return options.allrules.NotEqual.alertText;
}
}
Example:
sss2# and sss2# // shows error message
sss2# and SSS2# // no error message is shown
Please can anyone help. Both the values are user input.
Use the toLowerCase() function. That will convert your strings to all lower case characters, and the comparison will then return true whether or not there were differences in case originally:
if(password.toLowerCase() == username.toLowerCase()) {
//Do stuff
}
Convert both in lover case by and than compare
if(password.toLowerCase() == username.toLowerCase())
{
//sucess
}