티스토리 뷰

반응형

자바스크립트 강의를 보다가 이해 안가는 코드가 있었다. 바로.. !!

 

   return !!localStorage.getItem('token')
        

localStorage에서 token을 가지고 오는데, 앞에 '!!'는 무엇이란 말인가..

스택오버플로우에서 찾아보니 아래와 같은 의미라고 한다. 

Object to boolean.
If it was falsey (e.g. 0, null, undefined, etc.), it will be false, otherwise, true.


boolean으로 형변환 해주는 역할을 한다. 0 또는 null 또는 undefined 일때는 false를

그외에는 모두 true를 리턴해준다. 

ex)

var intTest = 10;
var nullTest = null;
var emptyTest = '';

console.log(!!intTest);
> true
console.log(!!nullTest);
> false
console.log(!!emptyTest);
> false

출처 :  https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript

 

What is the !! (not not) operator in JavaScript?

I saw some code that seems to use an operator I don't recognize, in the form of two exclamation points, like so: !!. Can someone please tell me what this operator does? The context in which I saw ...

stackoverflow.com

 

반응형

'javascript > Javascript(jQuery)' 카테고리의 다른 글

[Alert]SweetAlert 사용하기  (1) 2020.07.18
[jQuery]날짜(Date)  (0) 2019.05.28
모바일웹에서 휴대폰 키보드 이벤트  (0) 2019.01.15
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함