산술연산자
// 출력방법
result = a + b ;
// 자바와 똑같이 +을 사용할 수 있음
document.write("result : " + result + "<br>");
result = a - b ;
//document에서 +대신 , 도 출력 가능
document.write("-연산 : " , result, "<br>");
result = a - b ;
// + 와 , 둘 다 동시에도 사용 가능
document.write("-연산 : " , result + "<br>");
result = a / b;
document.write("/연산 : ", result + '<br>');
document.write(result.toFixed(2), " : 소숫점 두자리에서 끊을게요! <br> ");
비교 연산자
<script !src="">
let a = 10;
let b = 3;
let result;
// js자료형은 타입을 가리지 않기 때문에 boolean값도 출력 가능
result = a > b;
document.write("a>b : ", result);
// 다시 정수를 담는 result로 바꿀 수 도 있다
result = 100;
document.write("<br>", result, "<br>");
</script>
// 작거나 같은지 확인 가능
result = a<=b;
document.write('a<=b : ' + result);
// 같지 않은지 확인 가능
result = a != b;
// alert(); : 경고창
// 여러 경고성 멘트를 출력해보기 (우선순위가 높기때문에 먼저 출력됨)
alert();
논리 연산자
// prompt() : 프롬프트에 띄우면 숫자가 자동으로 String으로 인식된다.
let lastName = prompt("당신의 성은 무엇입니까?");
let age = prompt("당신의 나이는 어떻게 되십니까?");
let result = age >= 20 && age<30 && lastName == "ko";
if(result){ //result == true
document.write("합격입니다");
}else{
document.write("불합격 입니다")
}
Prompt로 받은 값 정수로 변경
기본적으로 프롬프트로 넘어온 값은 "문자열" 구조이다
//prompt로 입력받은 값은 ""문자열 구조
let kor = prompt("국어");
//kor을 실제 정수로 바꿔주자
kor = Number(kor);
// 선언과 동시에 정수로 변경도 가능
let eng = Number(prompt("영어"));
let math = Number(prompt("수학"));
let result = kor + eng + math;
document.write('총점 : ', result, <br>);
document.write('평균 : ' , (result / 3 ).toFixed(2));
//소숫점 두자리에서 끊어줘
document.write("
<hr>");
//String(result) : 정수 -> 문자열
document.write(String(result) + 100);
</script>
삼항 연산자
//promt에는 자동 문자열로 들어가기 때문에 number로 변경해주어야함
let n1 = Number(prompt("값1"));
let n2 = Number(prompt("값2"));
n1 < n2 ? document.write("참") : document.write("거짓");
document.write("<hr>");
// if문 안에서 형변환을 지원해줌
// prompt로 값을 받으면 문장으로 값이 넘어오지만
// if문 등에서 비교연산자와 사용할 경우 알아서 정수형태로 인지
n1 = prompt("수1");
n2 = prompt("수2");
if (n1 >= 50 && n2 >= 50) {
document.write("합격");
} else {
document.write("불합격");
}
'JavaScript' 카테고리의 다른 글
[JavaScript] createElement : 요소(태그) 생성 (0) | 2023.03.02 |
---|---|
[JavaScript] form 중요한 태그!! (0) | 2023.03.02 |
[JavaScript] 메서드 / getByElementById / innerHTML (0) | 2023.02.28 |
[JavaScript] 반복문 (0) | 2023.02.27 |
[JavaScript] 자료형 (0) | 2023.02.27 |