javascript function returns as "null" instead of null value
javascript functions list
javascript function return value to variable
javascript return function object
javascript function return multiple values
function return - python
how to get value from one function to another in javascript
return function java
I have this javascript function:
getToken: function () { return localStorage.jwtToken; }, this.getToken()!=null //true,
so basically this function returns "null" instead of null, how can I make the function return the null value?
You probably did:
localStorage.jwtToken = null;
to set it to null
, but as localStorage
can only contain string properties, it got stringified to "null"
. Instead of setting it to null
set it to undefined
, or just delete the key:
localStorage.jwtToken = undefined; localStorage.removeItem("jwtToken");
JavaScript return Statement, A return statement determines the value the function returns. When control comes across such a statement, it immediately jumps out of the current function and Function Return. When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement. Functions often compute a return value.
If instead of "null"
you want to return null
, return this expression
getToken: function () { let token = localStorage.jwtToken; return (token === "null") ? null : token; }
JavaScript Functions, JavaScript allows functions to return functions, a very powerful technique. return [[expression]]; expression The expression whose value is to be returned. If omitted, undefined is returned instead. Description. When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.
this.getToken()!=null
returns true because you are only checking the value.
If you want to check the value and type you need to check with !==
.
this.getToken() !== null
Functions :: Eloquent JavaScript, Returning b is returning a function object. In Javascript, functions are just objects, like any other object. If you find that not helpful, just replace the Calling the function with in a return statement executes the function, and returns whatever value was returned by the function. It is similar to calling var x = b();, but instead of assigning the return value of b() you are returning it from the calling function a().
JavaScript Functions that Return Functions, Returning a value. A function can return a value back into the calling code as the result. The simplest example would be a JavaScript Return Statement JavaScript provides for passing one value back to the code that called it after everything in the function that needs to run has finished running. JavaScript passes a value from a function back to the code that called it by using the return statement. The value to be returned is specified in the return.
Functions that return a function, A function can optionally accept parameters, and returns one value. Functions in JavaScript are objects, a special kind of objects: To return a string from a JavaScript function, use the return statement in JavaScript.ExampleYou need to run the following code to learn how to return a string
Functions, This is known as invoking a function. Values can be passed into functions and used within the function. Functions always return a value. In JavaScript functions have a built-in object called the arguments object. The argument object contains an array of the arguments used when the function was called (invoked). This way you can simply use a function to find (for instance) the highest value in a list of numbers:
Comments
- Possible duplicate of Storing Objects in HTML5 localStorage
- How did you set the value? You've confirmed that
this.getToken()
returns"null"
?