From 68e49397c1a8a69c6d7ec334513f3090b2be1dea Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Mon, 17 Aug 2015 23:15:38 +0300 Subject: [PATCH] operators --- 1-js/2-first-steps/8-operators/article.md | 51 +++++++++-------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/1-js/2-first-steps/8-operators/article.md b/1-js/2-first-steps/8-operators/article.md index 7f8a884c..25197273 100644 --- a/1-js/2-first-steps/8-operators/article.md +++ b/1-js/2-first-steps/8-operators/article.md @@ -1,19 +1,17 @@ # Operators -Many operators are known to us from the school program. It is an addition `+`, a multiplication `*`, a subsctraction and so on. +Many operators are known to us from the school program. It is an addition `+`, a multiplication `*`, a substraction `-` and so on. -In this chapter we mainly concentrate on aspects not covered by the arithmetic. +In this chapter we concentrate on aspects that are not covered by the school arithmetic. [cut] ## Terms: "unary", "binary", "operand" -У операторов есть своя терминология, которая используется во всех языках программирования. - -Прежде, чем мы двинемся дальше -- несколько терминов, чтобы понимать, о чём речь. +Before we move on, let's make a dip in the terminology, to understand what we're talking about. -## Сложение строк, бинарный + +## Strings concatenation, binary + -Обычно при помощи плюса `'+'` складывают числа. +Usually the plus operator `'+'` sums numbers. -Но если бинарный оператор `'+'` применить к строкам, то он их объединяет в одну: +But if the binary `+` is applied to strings, it merges (concatenates) them: ```js -var a = "моя" + "строка"; -alert( a ); // моястрока +var s = "my" + "string"; +alert( s ); // mystring ``` -Иначе говорят, что "плюс производит конкатенацию (сложение) строк". +If one of operands is a string, then the other one is converted to string too. -**Если хотя бы один аргумент является строкой, то второй будет также преобразован к строке!** - -Причем не важно, справа или слева находится операнд-строка, в любом случае нестроковый аргумент будет преобразован. Например: +For example: ```js //+ run @@ -60,18 +58,6 @@ alert( '1' + 2 ); // "12" alert( 2 + '1' ); // "21" ``` -**Это приведение к строке -- особенность исключительно бинарного оператора `"+"`.** - -Остальные арифметические операторы работают только с числами и всегда приводят аргументы к числу. - -Например: - -```js -//+ run -alert( 2 - '1' ); // 1 -alert( 6 / '2' ); // 3 -``` - ### Преобразование к числу, унарный плюс + @@ -80,7 +66,8 @@ alert( 6 / '2' ); // 3 ```js //+ run -alert( +1 ); // 1 +alert( +1 ); + // 1 alert( +(1 - 2) ); // -1 ```