[Javascript] undefined와 null의 차이점이란 ?

undefined은 변수를 선언하고 값을 할당하지 않은 상태, null은 변수를 선언하고 빈 값을 할당한 상태(빈 객체)이다. 즉, undefined는 자료형이 없는 상태이다.
따라서 typeof를 통해 자료형을 확인해보면 null은 object로, undefined는 undefined가 출력되는 것을 확인할 수 있다.

typeof null // 'object'
typeof undefined // 'undefined'
null === undefined // false
null == undefined // true
null === null // true
null == null // true
!null // true
isNaN(1 + null) // false
isNaN(1 + undefined) // true

#undefined

undefined는 원시값(Primitive Type)으로, 선언한 후에 값을 할당하지 않은 변수나 값이 주어지지 않은 인수에 자동으로 할당된다. 이 값은 전역 객체의 속성 중 하나로, 전역 스코프에서의 변수이기도 하다. 따라서 undefined 변수의 초기 값은 undefined 원시 값이다.

cf) undefined는 예약어가 아니기 때문에, 전역 범위 외에서 변수 이름으로 사용할 수 있다. 그러나 유지보수와 디버깅에 어려움을 겪을 수 있으므로 피하는 것이 좋다.

아래의 경우에 변수가 undefined를 반환한다.

  • 값을 할당하지 않은 변수
  • 메서드와 선언에서 변수가 할당받지 않은 경우
  • 함수가 값을 return 하지 않았을 때

#null

null은 원시값(Primitive Type) 중 하나로, 어떤 값이 의도적으로 비어있음을 표현한다. undefined는 값이 지정되지 않은 경우를 의미하지만, null의 경우에는 해당 변수가 어떤 객체도 가리키고 있지 않다는 것을 의미한다.

cf) null은 undefined처럼 전역 객체의 속성 중 하나가 아니라 리터럴 값이다.

#알아두면 좋은 것

  • typeof undefined는 출력하면 undefined이다.
  • typeof null은 출력하면 object이다. 하지만 이는 여전히 원시 타입(primitive value)로, JavaScript에서는 구현 버그로 간주한다.
  • undefined == null은 true이다.

 

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

참조 : https://2ssue.github.io/common_questions_for_Web_Developer/docs/Javascript/13_undefined&null.html#undefinedhttps://2ssue.github.io/common_questions_for_Web_Developer/docs/Javascript/13_undefined&null.html#undefine

 

[출처] https://99geo.tistory.com/69

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
60 Is there a limit of vertices in WebGL? 웹지엘의 제약사항 졸리운_곰 2017.08.16 554
59 JavaScript Performance Monitor file 졸리운_곰 2017.08.16 371
58 Monitor Rendering Performance Within Three.js file 졸리운_곰 2017.08.16 541
57 List of WebGL frameworks 졸리운_곰 2017.08.16 500
56 From Unity to Three.js file 졸리운_곰 2017.08.16 1750
55 Collada dae to three.js json convertor sample file 졸리운_곰 2017.08.16 579
54 THREE.WebGLShader: Shader couldn't compile - Chrome Version 44.0.2403.125 #6929 file 졸리운_곰 2017.08.10 558
53 Uniforms types 졸리운_곰 2017.08.10 547
52 Custom shaders with Three.JS: Uniforms, textures and lighting file 졸리운_곰 2017.08.10 699
51 WebGL and ThreeJS Using Blender Models file 졸리운_곰 2017.08.05 357
50 Importing a Modeled Mesh From Blender to Three.js file 졸리운_곰 2017.08.05 458
49 Using SketchUp Models OrcaXS edited this page on 27 Apr · 7 revisions 졸리운_곰 2017.08.05 459
48 Three.js Loading .mtl and .obj - object stays white file 졸리운_곰 2017.08.05 541
47 [three.js] How to Build a First Person Shooter in the Browser with Three.js and WebGL/HTML5 Canvas file 졸리운_곰 2017.07.22 534
46 [three.js] WebGLRenderer 졸리운_곰 2017.07.22 490
45 Clone an Object3D model from a Collada load call 졸리운_곰 2017.07.22 345
44 [three.js] Quaternion 4원수, 사원수 회전 졸리운_곰 2017.07.08 919
43 COLLADA, TinyXML, and OpenGL file 졸리운_곰 2017.07.06 502
42 [github][gist] Mouse-Picking Collada Models with THREE.js file 졸리운_곰 2017.07.06 584
41 Mouse-Picking Collada Models with three.js, Part II file 졸리운_곰 2017.07.06 338
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED