Determining if a Variable Has Been Defined

Asked 2 months ago, Updated 2 months ago, 3 views

For example, to determine if data.value has been defined,

 if(typeof(data)!=='undefined'&typeof(data.value)!=='undefined'){
    console.log('defined:'+data.value);
}

I understand that there is a way to describe it more succinctly.

javascript typescript

2022-09-30 11:38

3 Answers

I am at a loss to decide whether the question is "variable defined" or "property defined", but I will answer from both perspectives.

 if(typeof(data)!=='undefined'&typeof(data.value)!=='undefined'){
    console.log('defined:'+data.value);
}

This code does not work as expected when data===null.

var data=null;
typeof(data.value)!=='undefined'; // TypeError: Cannot read property 'value' of null  

The null and undefined types do not have primitive wrapper objects and cannot have properties.

Use the in operator to determine the existence of properties including prototype chains such as your publication, except for Undefined and Null types that do not have the property.

'use strict';
function hasProperty(object,propertyName){
  return object != null&propertyName in Object(object);
}

console.log (hasProperty({a:1}, 'a')); // true
console.log(hasProperty({a:undefined}, 'a')); // true
console.log(hasProperty(', 'split')); // true
console.log(hasProperty(Object.create(null), 'a')); // false
console.log(hasProperty(null, 'a')); // false
console.log(hasProperty(undefined, 'a')); // false
console.log(hasProperty(data, 'a')); // ReferenceError: data is not defined  

As you can see from the previous code, if the variable containing the object is undefined, it will be ReferenceError
You must use the typeof operator to avoid ReferenceError, but even if you function the typeof operator, ReferenceError occurs at the time of function call.
To accommodate undefined variables, you must use the typeof operator before calling the function.

'use strict';
function hasProperty(object,propertyName){
  return object != null&propertyName in Object(object);
}

console.log(typeofdata!=='undefined' & hasProperty(data, 'a'));//false

Dear Re:jirolabo,


2022-09-30 11:38

If you want to exclude inherited properties, why not use hasOwnProperty

https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty


2022-09-30 11:38

How about this kind of kanji?

var data={};

function checkObject(){
  if(typeof(data)!=='undefined'&typeof(data.value)!=='undefined'){
      console.log('(Default) defined:'+data.value);
  }
  
  if('value' in data) {
      console.log('(object in) defined:'+data.value);
  }
  
  if(data.hasOwnProperty('value'){
      console.log('(hasOwnProperty)defined:'+data.value);
  }
}

function setValue() {data.value='test data';}
function clearValue() {delete data.value;} 
<button onclick="checkObject();">check</button><br/>
<button onclick="setValue();">setvalue</button>br/>
<button onclick="clearValue();">clear value</button>


2022-09-30 11:38

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.