반응형

[javascript] null, undefined, 공백 체크

 

// 넘어온 값이 빈값인지 체크합니다.
// !value 하면 생기는 논리적 오류를 제거하기 위해
// 명시적으로 value == 사용
// [], {} 도 빈값으로 처리
var isEmpty = function(value){
  if( value == "" || value == null || value == undefined || ( value != null && typeof value == "object" && !Object.keys(value).length ) ){
    return true
  }else{
    return false
  }
};
var value = 1; // Number 형 1 
console.log(value, typeof value); // 1 

number value = "1" // String 형 1 
console.log(value, typeof value); // 1 string
 
 
var value2 = "" 
if( value2 == ""){ 
    console.log("비어 있음"); 
}else{ 
    console.log("값이 있음"); 
}
 

 

반응형
반응형

[jQuery]  typeof 어떻게 사용하나? 자주 쓰는데.


undefined 때문에 엄청 귀찮을때 많다.








반응형
반응형


자바 스크립트에서 null 과 undefined 를 체크

 

typeof 연산자는 형식 정보를 다음 중 하나의 문자열로 반환해준다.


number, string, boolean, object, function, undefined

 

EX : if ( typeof a == "undefined" ) xxxx 실행


 

 

반응형

+ Recent posts