Javascript
Javascript - 틸트 연산자 (~) 물결 / (~~)
Back
2019. 3. 11. 19:01
Tilt (틸트) '~'
c언어와 마찬가지로 비트로 부정하는 연상자 -(n+1) 이런 식이 성립 문자열 메서드인
indexOf()로 논리식을 짤 수 있다.
1 2 3 4 5 6 7 | var str = '강남역,잠실역,신논현역'; var val = '남'; if (~str.indexOf(val)) { // 해당 문자열이 있다면 console.log(!~ str.indexOf(val)); // 이코드를 실행하라. } else { console.log(222); } | cs |
indexOf()
.indexOf()란 String의 몇 번째에 문자열이 존재하는지 계산해주는 함수이다.
결과 > "The index of the first "dog" from the beginning is 40" > "The index of the 2nd "dog" is 52" 즉, The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy? 라는 문자열에 'dog'라는 문자열은 몇 번째 칸에 있는지 구해주는것이다. 아무것도 찾지 못했다면 -1 을 반환한다. 참고 : https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf |
~str.indexOf(val)
- 있다면 -(n+1)의 값을 반환한다.
- 없다면 0을 반환한다.
1 2 3 4 5 6 7 | var val2 = '시'; if (!~str.indexOf(val2)) { // val2가 없다면 console.log(~ str.indexOf(val2)); // 이 코드를 실행하라. } else { console.log(222); } | cs |
~~ 연산자
Math.floor() 와 동등하게 쓰이는 연산자
1 2 | var num = '2234.5678'; console.log(~~num); // 2234 | cs |
Math.floor()
floor()는 Math의 정적 메소드 소수점을 버리는 용도로 쓰인다. 음수일때 -1
Math 객체를 생성하여 메소드를 사용하기보다, 항상 Math.floor()로 사용해야한다. (Math는 생성자가 아님)
|
출처 : https://webclub.tistory.com/21