1

I understand that the answer to this is it's in the specs but what, if any, is the logic behind it?

Welcome to Node.js v19.0.0.
Type ".help" for more information.
> !!''
false
> !![]
true

A String, to my understanding, is really an array with some extra functionality added to make text easier to work with, it still does all the array-like things though so why is an empty string falsey but an empty array is truthy?

4
  • 4
    A string is a primitive type and an array is an object. Objects are always truthy. That is a simple rule. Commented Jul 7, 2023 at 9:29
  • null is an object, too. but not truthy. Commented Jul 7, 2023 at 9:30
  • 1
    null is not an object, but a primitive. It has typeof equal to "object", but that is a different story. Commented Jul 7, 2023 at 9:30
  • Strings act in a similar way to arrays, but, in javascript at least, they are not arrays. eg you can reference a character with mystring[0] but you can't add characters with mystring.push("end"). Note that strings are immutable. See this question for more info on strings. Commented Jul 7, 2023 at 9:32

1 Answer 1

-1

Let's make it simple:

!!''

An empty string in JavaScript is considered a "falsy" value. It is treated as a boolean false when evaluated in a boolean context.

!![]

Arrays, whether it contains items or not, are considered "truthy" values in Javascript. They are treated as a boolean true when evaluated in a boolean context.

Sign up to request clarification or add additional context in comments.

3 Comments

This appears to just repeat the question - statement of fact rather than reasons.
shall I say, it's by definition that, objects are truthy, empty strings are falsey? I thought I already mentioned the logic, which is the OP is asking.
OP isn't asking about the "logic" of true/false, OP is asking about the philosophical logic - ie the reasoning of why it works like this. OP already stated they know it does work like this. They key is the phrase "the logic behind it"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.