# 조건문

## 조건문

### 조건문의 문법

#### boolean

#### :point\_right: **불린(Boolean) 참(true )와 거짓(false)** 두가지 밖에 없다. 이것이 핵심적인 역활을 담당한다.&#x20;

#### if

```javascript
if(true){
   alert ('result : true ');
   // 실행
}

if(flase){
   alert ('result : true ');
} // 실행되지 않
```

#### else

```javascript
//else if
if (false){
alear(1);
}
else if (true){
alert(2); //2 출력 
}
else (true){
alert(3);
}

//else 
if (false){
alear(1);
}
else if (false){
alert(2);  
}
else (true){
alert(3); //3출력
}
```

* **else if** 는 조건문은 **좀 더 다양한 케이스의 조건으로 검사**할 수 있고, **여러개**가 올 수 있다.
* **if문의 조건**이 true라면 if의 중괄호 구간이 실행되고, **false라면 else 이후의 중괄호 구간이 실행**

### 변수와 비교연산자&#x20;

```javascript
id = prompt ('아이디를 입력해주세요.');
if(id == 'egoing'){
    var password = prompt('비밀번호를 입력해주세요');
 if (password = '111111'){
    alert('로그인 했습니다.' +id+'님 반갑습니다.' );
 }else{
    alert('비밀번호가 다릅니다.');
 }
 }else{
 alert('아이디가 일치하지 않습니다.');
}
```

* prompt () 구문은 사용자가 입력한 값을 가져와서 id 변수의 값을로 대입함
* 조건문의 중첩 ->  if 문 안에 다시 if문을 사용하여 아이디의 값이 일치하는지 확인한 후에 비밀번호 가 일치하는지 확인하는것 이다. (조건문안에 중첩해서 사용될 수 있다.)&#x20;

### 논리연산

### &&

```javascript
// 논리 연산자를 사용하여 아이디 비밀번호 확인 
id = prompt ('아이디를 입력해주세요.');
if(id == 'egoing' && password = '111111'){
    alert('인증했습니다.');
 }else{
    alert('인증에 실패 했습니다.');
 }

```

* **좌항과 우항이 true**일때 참이 된다. 논리연산자는 and 연산자이다.&#x20;
* id가 egoing , password 가 111111 이면 참이된다 -> 좌항 우항이 모두참일때 전체가 참&#x20;

### ||

```javascript
id = prompt ('아이디를 입력해주세요.');
if (id=== 'eging' || id === 'k8805' || id=== 'sorialgi'){
 alert('인증 했습니다.');
}else{
 alert('인증에 실패 했습니다.')
}
```

* **||** 는 **좌우항 중에 하나라도 true** 라면 true가 되는 논리 연산자 or 연산자라고도 한다.

### !

```javascript
if(!true && !true){
alert(1); // 실행안
}

if(!false && !true){
alert(1); // 실행안
}

if(!false && !false){
alert(1); // 실행
}
```

**!** 는 부정의 의미로, Boolean 의 **값을 역전** 시킨다. false -> true,  true -> false 로 만든다.&#x20;

### boolean의 대체

### 1&#x20;

```javascript
if(0){
 alert(1); // 실행되지않음 false
}

if(1){
 alert(1); // 실행됨 true
}
```

1 -> true    0 -> false 의 값으로 간주된다.&#x20;

### false로 간주되는 데이터 형&#x20;

```javascript
if (!''){
 alert ('빈 문자열'); // 실행되지 않음 (빈문자열 -> false)
}

if(!undefined){
 alert('undefined'); // 실행되지 않음 
}

var a; 

if (!a){
 alert('값이 할당되지 않은 변수'); // 실행됨 true -> false false -> true 
}

if(!null){
 alert('null') //실행이됨 !-> 반대이기때문에 
}


if(!NaN){
 alert('NaN') //실행이됨 !-> 반대이기때문에 
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leeboa.gitbook.io/study/undefined-13/undefined-3.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
