본문 바로가기

Front-End/

Section3 : Types and Operators

Types


 

- 자바스크립트에서 타입을 정하지 않고 할당하는거 실화야?

 var test="str"; test=1

javascript engine 실행시점에 알아서 할당을 해준다. ( 그럼 string, number 타입 상관없이 메모리 크기는 똑같이 할당하는건가? 위의 상황에서 )

primitive types

 1) undefined

 2) null

 3) boolean

 4) number

 5) string

 6) symbol ( es6에서 나옴, 다음 글에서 소개 )


비교 연산자

== : value만 비교

=== : type과 value 비교
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness )


Default values


 

- 자바와 다른 문법이 있다...

자바를 토대로 예상을 하면 컴파일 에러가 나겠지만 null이 넘어갔다고 가정하면, Hello true만 출력될 것이다. 

 하지만 javascript에서는?

 Hello <Your Name>이 출력이 된다.

 || 가 true,false 개념 뿐만 아니라 변수가 할당되어 있지 않은경우 할당된걸 return 해준다.


 예 제

[ index.html ( app.js : console.log(value) / app1.js : var value="app1" ]

출력이 뭐라고 될까? 

"app2"가 나온다. app1.js가 먼저 global execution context에 value를 할당하고 app2.js가 value를 뒤집어씌운다.  
( 결론은 전역변수를 남용하지 말라! )