다연이네

[days02] js 연산자 본문

Web/JavaScript

[days02] js 연산자

 다연  2020. 12. 10. 18:04
반응형

1. js 연산자 - 자바랑 매우 비슷하다.

 

- 산술연산자

<script>
var x=10, y=20;
document.write(x++); //10
document.write("<br>");	
document.write(x--); //11
document.write("<br>");
document.write(x+y); //30
document.write("<br>");
document.write(x-y); //-10
document.write("<br>");
document.write(x*y); //200
document.write("<br>");
document.write(x/y);//0.5
document.write("<br>");
document.write(x%y); //10 
document.write("<br>");


document.write(2**3); //8
document.write("<br>");
 	
</script>

 

- 복합대입연산자  +=, -=, *=, /=, %=, **=

<script>
var x = 100;
x+=5;
document.write(x+"<br>"); //105

document.write(10%0+"<br>"); //NaN
document.write(3.1%0+"<br>"); //NaN

document.write(3.1/0+"<br>"); //Infinity
document.write(3.1/0+"<br>"); //Infinity
</script>

숫자%0 = NaN

숫자/0 = Infinity

 

- 숫자+문자

<script>
document.write(5+5+"<br>"); // 10 (앞의+는 덧셈연산자)
document.write(5+"5"+"<br>"); // 55 (앞의+는 숫자+문자열 = 문자열 연결 연산자)
document.write(5+1+"5"+"<br>"); // 65 
document.write(5+"1"+5+"<br>"); // 515 
</script>

 

- 비교연산자

<script>
document.write(5==10); //false
document.write(5===10); //false
document.write(5!=10); //true
document.write(5!==10); //true 
document.write(5>10); //false
document.write(5<10); //true
document.write(5>=10); //false
document.write(5<=10);	//true
</script>

==는 값만 같으면 된다. (!=는 값만 다르면 된다.)

===의 의미는 값도 같아야하고 타입(자료형)도 같아야 한다

 

-논리연산자

<script>
document.write(true&&true); //true
document.write(true&&false); //false
document.write(false&&true); //false
document.write(false&&false); //false
</script>

 

- typeof 타입을 반환하는 연산자

- 비트연산자

<script>
var x = 10;
alert(typeof x); //number
var x= 3.14;
alert(typeof x); //실수도 number
var x = 'admin';
alert(typeof x); //string
var x= true;
alert(typeof x); //boolean
</script>

<script> //비트연산자
//& | ~ ^ << >> >>>
//js에도 있구나 기억 (쓸일은 거의 없다)
</script>

- 기타 연산자

<script>

//. 연산자 존재		person.name  

var n = [];
alert(typeof n); //object

var n =function(){}; //함수도 js에서는 하나의 타입(자료형)이다
alert(typeof n); //function

//new 연산자		new Date()
//? : 삼항연산자도 존재
</script>
반응형
Comments