--- title: 자바스크립트의 기본적인 연산 - 숫자와 연산자 slug: Learn/JavaScript/First_steps/Math tags: - 연산자 - 자바스크립트 - 진수 translation_of: Learn/JavaScript/First_steps/Math ---
이 장은 자바스크립트의 연산에 대해 다룹니다. 능숙하게 숫자를 다루기 위해 어떻게 {{Glossary("Operator","operators")}} 와 그 외 기능을 사용하는지 알아봅니다.
필요조건: | 기본적인 커퓨터 지식, HTML과 CSS에 대한 기본적인 이해, JavaScript의 이해 |
---|---|
목표: | 자바스크립트 연산에 익숙해지기 |
좋아요, 아닐수도 있습니다. 일부는 좋아하겠죠. 몇몇 사람들은 구구단을 배울 때부터 싫어했을 겁니다. 그리고 몇몇은 이 사이 어딘가 있겠죠. 그러나 누구도 수학이 삶을 살아가는데 필수적 요소라는건 부정할 수 없을겁니다. 우리가 자바스크립트(혹은 다른 프로그래밍 언어)를 배울때 특히 그렇습니다. 우리가 하는 일의 상당 부분은 수치형 데이터를 처리하고, 값을 계산하거나 하는 일에 의지합니다.
이 장은 당장 알아야 하는 부분만 다룹니다.
프로그래밍에서, 우리가 잘 알고있는 십진법 체계라도 당신이 생각한 것보다 복잡합니다.
자바스크립트는 심지어 다른 숫자 타입을 지원합니다. 10진수는 10을 기준으로 합니다. (숫자 0~9을 쓴다는 뜻입니다.), 하지만 자바스크립트는 아래와 같은 데이터 타입을 지원합니다.
어려워서 힘들다고 느끼기 전에, 잠시 멈추세요! 시작하기 위해서 우리는 이제부터 10진수만 사용하도록 하겠습니다. 다른 유형에 대해 생각할 필요가 없습니다.
두 번째 좋은 소식은 다른 프로그래밍 언어와 달리, 자바스크립트는 실수와 정수 모두 {{jsxref("Number")}}라는 하나의 데이터 타입만 사용합니다. 실수와 정수 모두 같은 데이터 타입이기 때문에 하나의 방법으로 동작하게 할 수 있다는 뜻입니다.
우리가 필요한 기본 구문을 다시 익히기 위해 몇 가지 숫자를 빠르게 생각해 봅시다. developer tools JavaScript console 에 들어가서 아래의 나열된 명령어를 입력해주세요.
var myInt = 5; var myFloat = 6.667; myInt; myFloat;
typeof myInt; typeof myFloat;
"number"
는 정수와 실수인 경우에 나옵니다. — 이것들은 정수와 실수가 다른 데이터 타입일 때 보다 다루기 쉽게 해줍니다. 그리고 다른 데이터 타입일 때 다른 방법으로 다뤄야만 합니다. 호우~!산술연산자들의 기본적인 용도는 덧셈을 할 때 입니다.
Operator | Name | Purpose | Example |
---|---|---|---|
+ |
Addition | 두 개의 숫자를 더합니다. | 6 + 9 |
- |
Subtraction | 왼쪽에 있는 수를 오른쪽 수로 뺍니다. | 20 - 15 |
* |
Multiplication | 두 개의 숫자를 곱합니다. | 3 * 7 |
/ |
Division | 왼쪽의 숫자를 오른쪽 숫자로 나눠서 몫을 구합니다. | 10 / 5 |
% |
Remainder (sometimes called modulo) |
왼쪽의 숫자를 오른쪽 숫자로 나눠서 나머지를 구합니다. |
8 % 3 ( 2를 반환합니다, 8을 3으로 나눴을 때 몫이 2이기 때문입니다.) |
팁: 연산에 관계된 수를 피연산자라고 부릅니다.
참고 - {{Glossary("Operand", "operands")}}.
아직 수학을 공부할 필요는 없습니다. 하지만 우리는 문법 확인을 해야합니다. 아래의 명령어들을 developer tools JavaScript console 에 입력해주세요.
10 + 7 9 * 8 60 % 3
var num1 = 10; var num2 = 50; 9 * num1; num2 / num1;
5 + 10 * 3; num2 % 9 * num1; num2 + num1 / 8 + 2;
Some of this last set of sums might not give you quite the result you were expecting; the below section might well give the answer as to why.
Let's look at the last example from above, assuming that num2
holds the value 50 and num1
holds the value 10 (as originally stated above):
num2 + num1 / 8 + 2;
As a human being, you may read this as "50 plus 10 equals 60", then "8 plus 2 equals 10", and finally "60 divided by 10 equals 6".
But the browser does "10 divided by 8 equals 1.25", then "50 plus 1.25 plus 2 equals 53.25".
This is because of operator precedence — some operators will be applied before others when calculating the result of a sum (referred to as an expression, in programming). Operator precedence in JavaScript is the same as is taught in math classes in school — Multiply and divide are always done first, then add and subtract (the sum is always evaluated from left to right).
If you want to override operator precedence, you can put parentheses round the parts that you want to be explicitly dealt with first. So to get a result of 6, we could do this:
(num2 + num1) / (8 + 2);
Try it and see.
Note: A full list of all JavaScript operators and their precedence can be found in Expressions and operators.
Sometimes you'll want to repeatedly add or subtract one to/from a numeric variable value. This can be conveniently done using the increment (++
) and decrement(--
) operators. We used ++
in our "Guess the number" game back in our first splash into JavaScript article, when we added 1 to our guessCount
variable to keep track of how many guesses the user has left after each turn.
guessCount++;
Note: They are most commonly used in loops, which you'll learn about later on in the course. For example, say you wanted to loop through a list of prices, and add sales tax to each one. You'd use a loop to go through each value in turn and do the necessary calculation for adding the sales tax in each case. The incrementor is used to move to the next value when needed. We've actually provided a simple example showing how this is done — check it out live, and look at the source code to see if you can spot the incrementors! We'll look at loops in detail later on in the course.
Let's try playing with these in your console. For a start, note that you can't apply these directly to a number, which might seem strange, but we are assigning a variable a new updated value, not operating on the value itself. The following will return an error:
3++;
So, you can only increment an existing variable. Try this:
var num1 = 4; num1++;
Okay, strangeness number 2! When you do this, you'll see a value of 4 returned — this is because the browser returns the current value, then increments the variable. You can see that it's been incremented if you return the variable value again:
num1;
The same is true of --
: try the following
var num2 = 6; num2--; num2;
Note: You can make the browser do it the other way round — increment/decrement the variable then return the value — by putting the operator at the start of the variable instead of the end. Try the above examples again, but this time use ++num1
and --num2
.
Assignment operators are operators that assign a value to a variable. We have already used the most basic one, =
, loads of times — it simply assigns the variable on the left the value stated on the right:
var x = 3; // x contains the value 3 var y = 4; // y contains the value 4 x = y; // x now contains the same value y contains, 4
But there are some more complex types, which provide useful shortcuts to keep your code neater and more efficient. The most common are listed below:
Operator | Name | Purpose | Example | Shortcut for |
---|---|---|---|---|
+= |
Addition assignment | Adds the value on the right to the variable value on the left, then returns the new variable value | x = 3; |
x = 3; |
-= |
Subtraction assignment | Subtracts the value on the right from the variable value on the left, and returns the new variable value | x = 6; |
x = 6; |
*= |
Multiplication assignment | Multiples the variable value on the left by the value on the right, and returns the new variable value | x = 2; |
x = 2; |
/= |
Division assignment | Divides the variable value on the left by the value on the right, and returns the new variable value | x = 10; |
x = 10; |
Try typing some of the above examples into your console, to get an idea of how they work. In each case, see if you can guess what the value is before you type in the second line.
Note that you can quite happily use other variables on the right hand side of each expression, for example:
var x = 3; // x contains the value 3 var y = 4; // y contains the value 4 x *= y; // x now contains the value 12
Note: There are lots of other assignment operators available, but these are the basic ones you should learn now.
In this exercise, you will manipulate some numbers and operators to change the size of a box. The box is drawn using a browser API called the {{domxref("Canvas API", "", "", "true")}}. There is no need to worry about how this works — just concentrate on the math for now. The width and height of the box (in pixels) are defined by the variables x
and y
, which are initially both given a value of 50.
{{EmbedGHLiveSample("learning-area/javascript/introduction-to-js-1/maths/editable_canvas.html", '100%', 520)}}
In the editable code box above, there are two lines marked with a comment that we'd like you to update to make the box grow/shrink to certain sizes, using certain operators and/or values in each case. Let's try the following:
Don't worry if you totally mess the code up. You can always press the Reset button to get things working again. After you've answered all the above questions correctly, feel free to play with the code some more or create your own challenges.
Sometimes we will want to run true/false tests, then act accordingly depending on the result of that test — to do this we use comparison operators.
Operator | Name | Purpose | Example |
---|---|---|---|
=== |
Strict equality | Tests whether the left and right values are identical to one another | 5 === 2 + 4 |
!== |
Strict-non-equality | Tests whether the left and right values not identical to one another | 5 !== 2 + 3 |
< |
Less than | Tests whether the left value is smaller than the right one. | 10 < 6 |
> |
Greater than | Tests whether the left value is greater than the right one. | 10 > 20 |
<= | Less than or equal to | Tests whether the left value is smaller than or equal to the right one. | 3 <= 2 |
>= | Greater than or equal to | Tests whether the left value is greater than or equal to the right one. | 5 >= 4 |
Note: You may see some people using ==
and !=
in their tests for equality and non-equality. These are valid operators in JavaScript, but they differ from ===
/!==
. The former versions test whether the values are the same but not whether the values' datatypes are the same. The latter, strict versions test the equality of both the values and their datatypes. The strict versions tend to result in fewer errors, so we recommend you use them.
If you try entering some of these values in a console, you'll see that they all return true
/false
values — those booleans we mentioned in the last article. These are very useful, as they allow us to make decisions in our code, and they are used every time we want to make a choice of some kind. For example, booleans can be used to:
We'll look at how to code such logic when we look at conditional statements in a future article. For now, let's look at a quick example:
<button>Start machine</button> <p>The machine is stopped.</p>
var btn = document.querySelector('button'); var txt = document.querySelector('p'); btn.addEventListener('click', updateBtn); function updateBtn() { if (btn.textContent === 'Start machine') { btn.textContent = 'Stop machine'; txt.textContent = 'The machine has started!'; } else { btn.textContent = 'Start machine'; txt.textContent = 'The machine is stopped.'; } }
{{EmbedGHLiveSample("learning-area/javascript/introduction-to-js-1/maths/conditional.html", '100%', 100)}}
You can see the equality operator being used just inside the updateBtn()
function. In this case, we are not testing if two mathemetical expressions have the same value — we are testing whether the text content of a button contains a certain string — but it is still the same principle at work. If the button is currently saying "Start machine" when it is pressed, we change its label to "Stop machine", and update the label as appropriate. If the button is currently saying "Stop machine" when it is pressed, we swap the display back again.
Note: Such a control that swaps between two states is generally referred to as a toggle. It toggles between one state and another — light on, light off, etc.
In this article we have covered the fundamental information you need to know about numbers in JavaScript, for now. You'll see numbers used again and again, all the way through your JavaScript learning, so it's a good idea to get this out of the way now. If you are one of those people that doesn't enjoy math, you can take comfort in the fact that this chapter was pretty short.
In the next article, we'll explore text and how JavaScript allows us to manipulate it.
Note: If you do enjoy math and want to read more about how it is implemented in JavaScript, you can find a lot more detail in MDN's main JavaScript section. Great places to start are our Numbers and dates and Expressions and operators articles.
{{PreviousMenuNext("Learn/JavaScript/First_steps/Variables", "Learn/JavaScript/First_steps/Strings", "Learn/JavaScript/First_steps")}}