자바스크립트에서 null 과 undefined 값에 대해서 > script

본문 바로가기
사이트 내 전체검색

script

자바스크립트에서 null 과 undefined 값에 대해서

페이지 정보

작성자 서방님 댓글 0건 조회 138회 작성일 08-04-10 11:17

본문

 

질문) alert("" == 0) ;

        이렇게 하면 true , false 둘중 어느것이 나올까요?

  답은 true 입니다. 이상하지 않나요?

  if("" == 0 ) // with casting

  if("" === 0 )  // without casting

  즉 ""이 형변환이 되어 0으로 되서 true로 되었습니다.
  또  alert("" === 0) ;  이면  false가 됩니다.

 그리고 ""이 형변환해서 0이 된 이유는 아래와 같습니다.

 자바스크립트에서 null과 undefined의 차이점을 알아야 하는데요

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

var TestVar;
alert(TestVar);                   //shows undefined
alert(typeof TestVar);          //shows undefined

null is an assignment value. It can be assigned to a variable as a representation of no value:

var TestVar = null;
alert(TestVar);             //shows null
alert(typeof TestVar);    //shows object

From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

Unassigned variables are initialized by JavaScript with a default value of undefined.

JavaScript never sets a value to null. That must be done programmatically.

As such, null can be a useful debugging tool. If a variable is null, it was set in the program, not by JavaScript.

null values are evaluated as follows when used in these contexts:

Boolean: false
Numeric: 0
String: “null”

undefined values are evaluated as follows when used in these contexts:

Boolean: false
Numeric: NaN
String: “undefined”

댓글목록

등록된 댓글이 없습니다.

Total 846건 21 페이지
게시물 검색

회원로그인

접속자집계

오늘
122
어제
225
최대
1,347
전체
154,881
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.