Boolean variable returns as string from javascript function

anishthecoder

Why is it that when I'm returning, what I think is, a boolean variable from a javascript function, it is detected in the calling function as a string, but if I return a boolean literal, the calling function detects it as a boolean?

So, for example:

$( document ).ready(function(){
    $('#result').text(typeof validate());
    $('#result2').text(typeof validate2());
});

function validate(){
    status = true;
    status = false;
    return status;
}
    
function validate2(){
    return true;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Hello</p>
<div id="result"></div>
<div id="result2"></div>

Oriol

You don't declare the status status variable.

Therefore, the global one (window.status) is overwritten.

However, the HTML 5 spec defines that property as a DOMString:

interface Window : EventTarget {
  attribute DOMString status;
};

Therefore, it has a setter (either exposed or internal) which stores the stringified value.

To fix it, just declare your local variable using the var statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Function returns boolean not string

From Dev

I have a method that returns string however the variable is of type nullable boolean

From Dev

I have a method that returns string however the variable is of type nullable boolean

From Dev

JavaScript function sending string but receiving Boolean value

From Dev

Evaluate boolean expression from a variable in JavaScript

From Dev

Boolean method with string returns

From Dev

Regex returns a boolean not a string

From Dev

Boolean method with string returns

From Dev

Haskell: How to use random integer from randomRIO in a function that returns a boolean

From Dev

Javascript variable that always returns value of function

From Dev

Javascript variable assignment of object returns string

From Dev

Function returns always Boolean False

From Dev

Create JavaScript boolean variable

From Dev

getResource returns boolean instead String[]

From Dev

Return variable from Javascript function

From Dev

Return variable from Javascript function

From Dev

Send variable from function javascript

From Dev

return a variable from a function javascript

From Dev

Function that returns a string from an array of pointers

From Dev

How to assign a boolean value returned from a function to a variable in PostgreSQL 9.4?

From Dev

How do I compare the String returned from a function to another String variable in Javascript

From Dev

negate boolean function in javascript

From Dev

Javascript String to Boolean

From Dev

Javascript - convert string to boolean?

From Dev

How to pass String Variable in Javascript Function?

From Dev

Javascript using local variable for function property string

From Dev

Requiring app variable from electron inside function returns undefined

From Dev

isset() returns true from a string variable accessed as an array with any key

From Dev

Variable from string interpolation returns a non unit value

Related Related

  1. 1

    Function returns boolean not string

  2. 2

    I have a method that returns string however the variable is of type nullable boolean

  3. 3

    I have a method that returns string however the variable is of type nullable boolean

  4. 4

    JavaScript function sending string but receiving Boolean value

  5. 5

    Evaluate boolean expression from a variable in JavaScript

  6. 6

    Boolean method with string returns

  7. 7

    Regex returns a boolean not a string

  8. 8

    Boolean method with string returns

  9. 9

    Haskell: How to use random integer from randomRIO in a function that returns a boolean

  10. 10

    Javascript variable that always returns value of function

  11. 11

    Javascript variable assignment of object returns string

  12. 12

    Function returns always Boolean False

  13. 13

    Create JavaScript boolean variable

  14. 14

    getResource returns boolean instead String[]

  15. 15

    Return variable from Javascript function

  16. 16

    Return variable from Javascript function

  17. 17

    Send variable from function javascript

  18. 18

    return a variable from a function javascript

  19. 19

    Function that returns a string from an array of pointers

  20. 20

    How to assign a boolean value returned from a function to a variable in PostgreSQL 9.4?

  21. 21

    How do I compare the String returned from a function to another String variable in Javascript

  22. 22

    negate boolean function in javascript

  23. 23

    Javascript String to Boolean

  24. 24

    Javascript - convert string to boolean?

  25. 25

    How to pass String Variable in Javascript Function?

  26. 26

    Javascript using local variable for function property string

  27. 27

    Requiring app variable from electron inside function returns undefined

  28. 28

    isset() returns true from a string variable accessed as an array with any key

  29. 29

    Variable from string interpolation returns a non unit value

HotTag

Archive