Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast "null" as null in javascript

I use the session storage to set a key that can take numeric values or null. I noticed that the field is stored as a string no matter what I put in. Is there a nice way to convert back my stored value to null ?

For instance, I want to do a check in my code as such:

if (!sessionStorage.getItem('key')) {
   // do something
}

Whats the best way to make that check? The obvious way would be

 if (!sessionStorage.getItem('key') !== "null") {
       // do something
 }

I am wondering if there's an alternative.

like image 265
QuantumLicht Avatar asked Mar 10 '16 21:03

QuantumLicht


People also ask

How do I make null zero in JavaScript?

Use the ternary operator to convert a null value to zero, e.g. const result = val === null ? 0 : val . If the value is equal to null , the operator returns 0 , otherwise the value is returned.

What is null null in JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

How do you write null in JavaScript?

In JavaScript, null is a special value that represents an empty or unknown value. For example, let number = null; The code above suggests that the number variable is empty at the moment and may have a value later.

Can null be converted to string?

If we concatenate null with an empty string (""), the result will be of string type. We can use this technique to convert null to string. We use + operator to concatenate the null and empty string(""). Here is the example to convert null to string using this approach.


2 Answers

You can parse it

JSON.parse('null') // null
JSON.parse('1')   //  1

so

var val = sessionStorage.getItem('key');
val = JSON.parse(val);

Close to cast

Alternative would be:

var val = sessionStorage.getItem('key');
val = val*1 || null;
like image 185
kidwon Avatar answered Sep 28 '22 05:09

kidwon


I just want something that would evaluate to false

You could use an empty string instead to mark the unset value:

sessionStorage.setItem('key','');

This way you can re-use your current check:

if (!sessionStorage.getItem('key')) {
   // do something
}

That said, you mentioned you had a drop down to select values, and that's why you need such a value, but I think a solution involving deleting the item on the null value would be better. Here's a working demo, and the code used to make it:

var dd = document.getElementById('dropdown');

dd.addEventListener('change', function(){
  var value = this.value;
  if (value === 'null')
    sessionStorage.removeItem('key');
  else sessionStorage.setItem('key', value);
});

var check = document.getElementById('check');
check.addEventListener('click', function(){
    this.innerHTML = 'Value: '+sessionStorage.getItem('key')+
                     ', check: '+(!sessionStorage.getItem('key'));
});
<select id="dropdown">
  <option value="null">none</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

<button id="check">Check value</button>
like image 33
SeinopSys Avatar answered Sep 28 '22 03:09

SeinopSys