let isAudlt = ture; // 또는 false
let isStudent = false; // 또는 true
3 > 5; // false
9 < 10; // true
'hello' === 'world'; // false
비교의 결과는 늘 Boolean, 즉 true or false
비교 연산자
기호 | 내용 |
---|---|
> | 초과 |
< | 미만 |
>= | 이상 |
<= | 이하 |
=== | 같다 |
!== | 다르다 |
기호 | 내용 |
---|---|
== | 같다 |
!= | 다르다 |
예를 들어서
1 == '1' // true 숫자 1과 문자열 1이 같다라는 결과가 나온다
1 == ture // true 숫자 1과 boolean이 같다라는 결과가 나온다
JS Comparison Table을 통해서 이퀄사인 2개와 3개를 사용했을 때 결과값을 보기 좋게 표로 만들어놨다.
참고하면 좋을 것이다.
if (조건1) {
// 조건1이 통화할 경우
} else if (조건2) {
//조건1이 통화하지 안혹
//조건2가 통과할 경우
} else {
// 모든 조건이 통과하지 않는 경우
}
//조건에는 Boolean으로 결과가 나오는 조건문 표현식이 들어간다.
두가지 조건이 한번에 적용되는 경우
논리 연산자(Logical Operator)를 사용한다.
논리연산자의 유용한 예
isStudent && isFemale;
//학생이면서, 여성일 때 통과
//AND 연산자라 부른다.
isStudent || isFemale;
//학생이거나, 여성일 때 통과
//OR 연산자라 부른다.
//여기서 ||는 Enter키 위에 있다.
!isStudnet && isFemale;
//학생이 아니면서, 여성일 때 통과
//NOT 연산자라 부른다.
//truthy, falsy 여부를 반전시킨다.
!false // true
!(3>2) // false
!undefined // true undefined를 false로 취급(falsy)
!'Hello' // false 'Hello'를 true로 취급(truthy)
true || true // true
true || false // true
false || false // false
true && true // true
true && false // false
false && false // false
if (false)
if (null) //값이 없다는 의미
if (undefined) //정의되지 않았다는 것을 undefined라한다
if (0)
if (NaN) //Not a number
if ('') //아무런 값이 없는 문자열
var str = 'HelloWorld';
console.log(srt[0]); // 'H' 0번째인 H가 결과값으로 나온다.
console.log(str[4]); // 'o' 4번째인 o가 결과값으로 나온다.
console.log(str[10]); //undefined
str[0] = 'G'
console.log(str); // 'HelloWorld' not 'GelloWorld'
var str1 = 'Hello';
var str2 = 'World';
var str3 = '1';
console.log(str1 + str2); // 'HelloWorld' str1의 값 str2의 값을 플러스 연산자로 연결 시킬경우 연결된 문자열이 나온다.
consloe.log(str3 + 7); // '17' 문자열1과 숫자7을 더하면 8이 나오는게 아니라 문자열이 반환된다.
문자열의 전체 길이를 반환
var str = 'HelloWorld'
console.log(str.length); // 10
'Hello World'.indexOf('Hello'); // 0 0번째의 index에 Hello가 들어가기때문에 0을 출력
'Hello World'.indexOf('hello'); // -1 찾고자하는 문자열이 없기때문에 -1을 출력
'Hello World'.indexOf('World'); // 6 6번째의 index에 Hello가 들어가기때문에 6을 출력
'Hello World World'.indexOf('World') // 6 처음 등장하는 index만 출력을 한다
'canal'.lastIndexOf('r'); // 3 뒤에서부터 찾는 index r이 3번째이 있기 때문에 3을 출력
var str = 'Hello from the other side';
console.log(str.split(' ')); // 빈공백의 seperator를 이용해 잘라낸다.
//["Hello", "from", "the", "other", "side"]
var str = 'abcdefghij';
console.log(str.substring(0, 3)); // 'abc' 0,1,2를 가져온다 3은 포함하지 않는다.
console.log(str.substring(3, 0)); // 'abc' 순서가 바뀌어도 0,1,2를 가져온다.
console.log(str.substring(1, 4)); // 'bcd' 1,2,3을 가져온다.
console.log(str.substring(-1, 4)); // 'abcd', 음수는 0으로 취급
console.log(str.substring(0, 20)); // 'abcdefghij', index범위를 초과하면 마지막까지 가져온다.
console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toUpperCase()); // 'ALPHABET'