Questions tagged [truthiness]

In dynamically typed language, truthiness is a term used to describe a value that might evaluate to boolean true.

99 questions
1
vote
6 answers

how to check xpath expression (if valid/not) before scraping data with spider

Probably you have realized by title, I am using scrapy and xpath to extract data. I tried and provided xpaths from file to the spider (to make spider generic - not to edit often) As required, I am able to extract data in the format…
techDiscussion
  • 89
  • 3
  • 12
1
vote
1 answer

Object has value but does not appear to exist?

I have the following Python code: if bedrooms: # bedrooms exists ..... else: # BEDROOMS DOES NOT EXIST bn = "BEDROOMS DOES NOT EXIST" I was stepping through it in my debugger and noticed that even though bedroom == 0 , the flow jumps to…
user1592380
  • 34,265
  • 92
  • 284
  • 515
0
votes
1 answer

In environments that take Boolean arguments, is it a good idea to wrap all functions instead of allowing them to be implicitly coerced?

Take the String#=~ function for instance. It will return the index of the first match if the match is found, which, as a Fixnum will always act as true in boolean environments. If the match isn't found, it returns null, which acts as false. Now…
Steven
  • 17,796
  • 13
  • 66
  • 118
0
votes
4 answers

How can I create a Proc to checking whether the elements of an array are nil or not?

I'm trying to solve this assignment: Create a Proc that will check whether a given array of elements are nil or not. Use .nil? to evaluate elements. Print 'true' if the elements are nil and 'false' if the element has a [truthy] value. This is my…
Dev
  • 21
  • 4
0
votes
0 answers

.every() prototype isn't working as expected

Obviously it works for people so I must just be dumb, but I am passing an array of boolean's through the .every() prototype and it ALWAYS appears to return true, even when it shouldnt. so heres the code The function call is made checkAnswer(); Then…
0
votes
2 answers

Typescript optional function truthy check

I think I'm missing something basic on how you can check for a truthy value with && in Typescript. Why can props.function still be undefined in test1 and has to be checked like I did in test2. import React from "react"; interface PropsModel { …
Sean A.S. Mengis
  • 129
  • 2
  • 13
0
votes
1 answer

Checking lots of variables for truthiness in a single pass

I have a large form with many fields and a special processing function Several variables of different types declared at the beginning of a function I would like to check that a user has set all variables before a function execution const a1 = true,…
0
votes
0 answers

When I am testing for truthiness, why is my Object.create(null) value true while null is false?

I am trying to confirm for myself whether a null object is false. First, I tried to create a null object using Object.create(null). Then I decided to test this object against null. I wrote a snippet of code to check for truthiness: let emptyObject =…
0
votes
2 answers

Is !obj[key] a good practice to check the existence of an object's property in JavaScript?

Every now and then, I see this pattern: if (!obj[key]) { // key does not exist inside object obj[key] = ... } else { // maybe do something else with obj[key] } but I find it extremely wrong. What if actually obj[key] has the value false…
but-why
  • 533
  • 3
  • 10
0
votes
4 answers

Is //(typeof(myVar) != "undefined")// logically equivalent to the truthy //if (myVar)?

Over a large project I do a lot of exception handling by explicitly checking whether a variable has been set using typeof. It's a bit verbose and I'd like to change my habit to a simple truthy: if (myVar) {//do stuff} In the snippet below and in…
Mark Taylor
  • 1,128
  • 8
  • 15
0
votes
4 answers

comparing with `{}` and truthyness

I have an API that calls me with {} for a param that I need to replace. But having trouble as in ... can you guess what happens below? const log = console.log const a = {} log('a is ', a) // cannot compare with either == or === if (a == {}) { …
dcsan
  • 11,333
  • 15
  • 77
  • 118
0
votes
2 answers

How to run the logTruthiness function

This code given below is supposed to evaluate if a value coerces to true or false. How does this work? for example if I want to know if a string- "my string" will be coereced to true or false, how do I use the to determine that? I have tried to…
0
votes
1 answer

Python if statements and truth value testing for non compsci background

This aims to be a self answered question after a few hours of digging, I found this thought process may prove useful to others who also may not have come from a formal compsci background. This all started from confusion over why one particular if…
0
votes
2 answers

Bash: Safe to remove all occurrences of -n inside brackets?

I recently found this "bug" in my bash script; if [ "$var" ]; then echo "Condition is true" fi where what I meant was to check if $var is non-empty, i.e. [ -n "$var" ]. As it happens, the code seems to work perfectly fine without the -n.…
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
0
votes
2 answers

Anyone know why I'm getting a "Process finished with exit code 0"?

I'm trying to run this program but for some reason, whenever I enter 0, the program stops running. I expect a "You've guessed too low, please try again " as the input is less than the random number generated. Can anyone help and explain? Also, feel…
Mir
  • 55
  • 1
  • 9