-3

I am trying to convert a string to a boolean in javascript, but I dont know how to do it and the solutions from the internet dont seem to work

I have one variable called data

console.log(data)
console.log(data === "true")
console.log(typeof data)
console.log(typeof "true")

this is my output code

true
false
string
string

this is the output I also tried == but it isnt working too

update: I converted the strings to byte arrays

(12) [0, 116, 0, 114, 0, 117, 0, 101, 0, 13, 0, 10]
(8) [0, 116, 0, 114, 0, 117, 0, 101]

these are the two byte arrays. The upper one is my variable and the lower one is the array of a new created string "true". Can anybody tell where this difference comes from?

carlos.gmz
  • 100
  • 8
  • 2
    Tip: "JavaScript" or "JS" but *never* "Java script". – tadman May 25 '21 at 21:17
  • This needs a bit more information. Are you trying to find out if a string contains the word “True” or “False” and parse it into a Boolean? Or are you simply wanting to know if a string is truthy. As in if a string has content? Because they are differ things. – Johan Jarvi May 25 '21 at 21:17
  • 2
    What is your input? What values of `data` do you need to handle? – tadman May 25 '21 at 21:17
  • as input only exists "true" and "false". So I just dont understand why it isnt checking the string similarity right – carlos.gmz May 25 '21 at 21:19
  • Does this answer your question? [How can I convert a string to boolean in JavaScript?](https://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript) – zcoop98 May 25 '21 at 21:19
  • 2
    I think there are only few explanations on how this can happen, and they all start with "someone is deliberately trying to break your code", and include "you don't show enough to tell". This would be e.g. whitespace or odd unicode inside the string, self-mutating values, etc. – ASDFGerte May 25 '21 at 21:19
  • the data is send from a node js server to a js socket. But the unicode idea seems like a possiblity. maby I dodnt set the encoding eight at one point – carlos.gmz May 25 '21 at 21:22
  • @carlos.gmz There's not enough information in this question to help you. All we can say is that `data` doesn't equal the string "true", which you already know. You're going to have to use your developer tools to figure out what's actually in that variable. – Brad May 25 '21 at 21:23
  • @carlos.gmz are the 4 console log statements executed in order? – Alan May 25 '21 at 21:31
  • @Alan yes they are, but I made an update to the question, looks like it has something to do with the byteArray – carlos.gmz May 25 '21 at 21:31
  • I think u have some issue with your output -> you can try online editor, i did working fine for me -> https://onecompiler.com/javascript/3wyksrvtd – Chandan Kumar May 25 '21 at 21:32
  • 1
    Looks like your string has control characters in it. Specifically Carriage Return and Line Feed: https://www.petefreitag.com/item/863.cfm – Alan May 25 '21 at 21:32

2 Answers2

3

Based on your updated answer, data === "true" will check if your string is equals to "true", as expected.

The reason that you are getting false as an output, is that value of data does not strictly equal "true" but instead "true\r\n"

If you need to instead check if the string contains "true" you can use either a regex, or simple index.

console.log(data.indexOf("true") !== -1)

Alan
  • 45,915
  • 17
  • 113
  • 134
  • If he has ES6 he could also do: `data.includes('true')`. – Mordred May 25 '21 at 21:39
  • Okay, fair warning, if the string is "truest" or "truely" or "truefalse" `indexOf("true")` will return a positive number. You may wish to remove control characters if you MUST verify the string is strictly `true` but want to allow for control characters. – Alan May 25 '21 at 21:42
0
var val = 'true';
console.log(val.trim().toLowerCase()==='true');

Unless I'm missing something more obvious?

Erick Smith
  • 937
  • 7
  • 27