You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
-Větší/menší nebo rovno: <code>a >= b</code>, <code>a <= b</code>.
-Rovno: `a == b`. Prosíme všimněte si, že znak dvou rovnítek za sebou `==`znamenátest rovnosti, zatímco jediné rovnítko`a = b`znamená přiřazení.
-Nerovno: v matematice se používá znak <code>≠</code>,ale v JavaScriptu se píše <code>a != b</code>.
In this article we'll learn more about different types of comparisons, how JavaScript makes them, including important peculiarities.
V tomto článku se dozvíme víc o různých druzích porovnání a o tom, jak s nimi JavaScript zachází, včetně důležitých zvláštností.
At the end you'll find a good recipe to avoid "JavaScript quirks"-related issues.
Na konci najdete recept, jak se vyhnout různým problémům způsobeným JavaScriptovými „vtípky“.
##Boolean is the result
##Výsledkem je typ boolean
All comparison operators return a boolean value:
Všechny porovnávací operátory vracejí hodnotu typu boolean:
- `true` --means "yes", "correct" or "the truth".
- `false` --means "no", "wrong" or "not the truth".
- `true` --znamená „ano“, „správně“ nebo „pravda“.
- `false` --znamená „ne“, „nesprávně“ nebo „nepravda“.
For example:
Příklad:
```js run
alert( 2 > 1 ); // true (correct)
alert( 2 == 1 ); // false (wrong)
alert( 2 != 1 ); // true (correct)
alert( 2 > 1 ); // true (správně)
alert( 2 == 1 ); // false (nesprávně)
alert( 2 != 1 ); // true (správně)
```
A comparison result can be assigned to a variable, just like any value:
Výsledek porovnání můžeme přiřadit do proměnné, stejně jako každou jinou hodnotu:
```js run
letresult = 5 > 4; //assign the result of the comparison
alert(result ); // true
letvýsledek = 5 > 4; //přiřadíme výsledek porovnání
alert(výsledek ); // true
```
##String comparison
##Porovnávání řetězců
To see whether a string is greater than another, JavaScriptuses the so-called "dictionary" or "lexicographical" order.
Ke zjištění, zda je jeden řetězec větší než jiný, JavaScriptpoužívá tzv. „slovníkové“ neboli „lexikografické“ pořadí.
In other words, strings are compared letter-by-letter.
Jinými slovy, řetězce se porovnávají znak po znaku.
For example:
Příklad:
```js run
alert( 'Z' > 'A' ); // true
alert( 'Glow' > 'Glee' ); // true
alert( 'Bee' > 'Be' ); // true
```
The algorithm to compare two strings is simple:
Algoritmus pro porovnání dvou řetězců je jednoduchý:
1.Compare the first character of both strings.
2.If the first character from the first string is greater (or less) than the other string's, then the first string is greater (or less) than the second. We're done.
3.Otherwise, if both strings' first characters are the same, compare the second characters the same way.
4.Repeat until the end of either string.
5.If both strings end at the same length, then they are equal. Otherwise, the longer string is greater.
1.Porovnej první znak obou řetězců.
2.Je-li první znak prvního řetězce větší (nebo menší) než první znak druhého řetězce, je první řetězec větší (nebo menší) než druhý. Tím jsme hotovi.
3.V opačném případě, tedy jsou-li první znaky obou řetězců stejné, porovnej stejným způsobem druhé znaky.
4.Tento proces opakuj, dokud nedojdeš na konec některého z řetězců.
5.Mají-li oba řetězce stejnou délku, jsou si rovny. V opačném případě je delší řetězec větší.
In the first example above, the comparison`'Z' > 'A'`gets to a result at the first step.
Ve výše uvedených příkladech porovnání`'Z' > 'A'`dojde k výsledku hned v prvním kroku.
The second comparison `'Glow'` and `'Glee'` needs more steps as strings are compared character-by-character:
Porovnání řetězců `"Glow"` a `"Glee"` však vyžaduje více kroků, jelikož řetězce se porovnávají znak po znaku:
1. `G`is the same as `G`.
2. `l`is the same as `l`.
3. `o`is greater than `e`.Stop here. The first string is greater.
1. `G`je stejné jako `G`.
2. `l`je stejné jako `l`.
3. `o`je větší než `e`.Tady se zastavíme. První řetězec je větší.
```smart header="Not a real dictionary, but Unicode order"
The comparison algorithm given above is roughly equivalent to the one used in dictionaries or phone books, but it's not exactly the same.
```smart header="Není to skutečný slovník, ale pořadí podle Unicode"
Uvedený porovnávací algoritmus zhruba odpovídá tomu, jaký se používá k řazení ve slovnících nebo telefonních seznamech, ale není úplně stejný.
For instance, case matters. A capital letter`"A"`is not equal to the lowercase`"a"`.Which one is greater? The lowercase`"a"`.Why? Because the lowercase character has a greater index in the internal encoding tableJavaScriptuses (Unicode). We'll get back to specific details and consequences of this in the chapter <info:string>.
Například záleží na velikosti písmen. Velké písmeno`"A"`není rovno malému`"a"`.Které je větší? Malé`"a"`.Proč? Protože malá písmena mají v interní kódovací tabulce, kterouJavaScriptpoužívá (Unicode), vyšší index. Ke specifickým podrobnostem a důsledkům se vrátíme v kapitole <info:string>.
```
##Comparison of different types
##Porovnávání rozdílných typů
When comparing values of different types, JavaScript converts the values to numbers.
Když JavaScript porovnává hodnoty různých typů, převádí je na čísla.
For example:
Příklad:
```js run
alert( '2' > 1 ); // true,string '2'becomes a number 2
alert( '01' == 1 ); // true,string '01'becomes a number 1
alert( '2' > 1 ); // true,řetězec '2'se převede na číslo 2
alert( '01' == 1 ); // true,řetězec '01'se převede na číslo 1
From JavaScript's standpoint, this result is quite normal. An equality check converts values using the numeric conversion (hence`"0"`becomes`0`),while the explicit `Boolean`conversion uses another set of rules.
Z pohledu JavaScriptu je tento výsledek zcela normální. Test rovnosti převádí hodnoty pomocí konverze na čísla (proto se`"0"`převede na`0`),zatímco explicitní konverze `Boolean`funguje podle jiných pravidel.
````
##Strict equality
##Striktní rovnost
A regular equality check`==`has a problem. It cannot differentiate`0`from `false`:
Běžné ověření rovnosti`==`má problém. Nedokáže rozlišit`0`od `false`:
```js run
alert( 0 == false ); // true
```
The same thing happens with an empty string:
Totéž platí pro prázdný řetězec:
```js run
alert( '' == false ); // true
```
This happens because operands of different types are converted to numbers by the equality operator `==`. An empty string, just like `false`,becomes a zero.
To se děje proto, že operátor rovnosti `==` převádí operandy různých typů na čísla. Prázdný řetězec, stejně jako `false`,se převede na nulu.
What to do if we'd like to differentiate`0`from `false`?
Co máme dělat, jestliže chceme rozlišit`0`od `false`?
**A strict equality operator`===`checks the equality without type conversion.**
**Operátor striktní rovnosti`===`zkontroluje rovnost bez typové konverze.**
In other words, if `a`and `b`are of different types, then `a === b`immediately returns `false` without an attempt to convert them.
Jinými slovy, jsou-li `a`a `b`různých typů, pak `a === b`okamžitě vrátí `false`, aniž by se je pokusil konvertovat.
Let's try it:
Zkusme to:
```js run
alert( 0 === false ); // false,because the types are different
alert( 0 === false ); // false,protože typy jsou různé
```
There is also a "strict non-equality" operator`!==` analogous to `!=`.
Existuje i operátor „striktní nerovnosti“`!==`, který je analogický s `!=`.
The strict equality operator is a bit longer to write, but makes it obvious what's going on and leaves less room for errors.
Operátor striktní rovnosti se píše trošičku déle, ale díky němu je hned zřejmé, o co tady jde, a ponechává méně prostoru k chybám.
##Comparison with nulland undefined
##Porovnávání s nulla undefined
There's a non-intuitive behavior when`null`or `undefined`are compared to other values.
Chování při porovnávání`null`nebo `undefined`s jinými hodnotami není zcela intuitivní.
For a strict equality check `===`
:These values are different, because each of them is a different type.
U operátoru striktní rovnosti `===`
:Tyto hodnoty jsou různé, protože každá z nich je jiného typu.
```js run
alert( null === undefined ); // false
```
For a non-strict check `==`
:There's a special rule. These two are a "sweet couple": they equal each other (in the sense of`==`),but not any other value.
U operátoru nestriktní rovnosti `==`
:Platí zvláštní pravidlo. Tyto dvě hodnoty tvoří „zamilovanou dvojici“: jsou si navzájem rovny (ve smyslu`==`),ale nejsou rovny žádné jiné hodnotě.
```js run
alert( null == undefined ); // true
```
For maths and other comparisons `< > <= >=`
: `null/undefined`are converted to numbers: `null`becomes`0`,while `undefined`becomes `NaN`.
U matematických a jiných porovnání `< > <= >=`
: `null/undefined`se převádějí na čísla: `null`se převede na`0`,zatímco `undefined`se převede na `NaN`.
Now let's see some funny things that happen when we apply these rules. And, what's more important, how to not fall into a trap with them.
Nyní se podívejme na některé legrační jevy, k nimž dochází při použití těchto pravidel. A, což je důležitější, jak se s nimi nechytit do pasti.
###Strange result: null vs 0
###Podivný výsledek: null vs 0
Let's compare`null`with a zero:
Porovnejme`null`s nulou:
```js run
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) *!*true*/!*
```
Mathematically, that's strange. The last result states that "`null`is greater than or equal to zero", so in one of the comparisons above it must be`true`,but they are bothfalse.
Matematicky je to zvláštní. Poslední uvedený výsledek tvrdí, že „`null`je větší nebo rovno nule“, takže jedno z výše uvedených porovnání musí být`true`,ale obě jsou `false`.
The reason is that an equality check`==`and comparisons `> < >= <=`work differently. Comparisons convert `null`to a number, treating it as`0`.That's why(3) `null >= 0`is true and(1) `null > 0`is false.
Důvodem je, že test rovnosti`==`a porovnání `> < >= <=`fungují odlišně. Porovnání převedou `null`na číslo a zacházejí s ním jako s`0`.Proto(3) `null >= 0`platí a(1) `null > 0`neplatí.
On the other hand, the equality check`==`for `undefined`and `null`is defined such that, without any conversions, they equal each other and don't equal anything else. That's why(2) `null == 0`is false.
Naproti tomu rovnost`==`pro `undefined`a `null`je definována tak, že se bez jakýchkoli konverzí rovnají navzájem a nerovnají se ničemu jinému. Proto(2) `null == 0`neplatí.
###An incomparable undefined
###Neporovnatelné undefined
The value`undefined`shouldn't be compared to other values:
Hodnota`undefined`by neměla být porovnávána s jinými hodnotami:
```js run
alert( undefined > 0 ); // false (1)
alert( undefined < 0 ); // false (2)
alert( undefined == 0 ); // false (3)
```
Why does it dislike zero so much? Always false!
Proč ji porovnávání nemá tak rádo? Vždycky vyjde false!
We get these results because:
Tyto výsledky jsme získali, protože:
-Comparisons `(1)`and `(2)`return `false` because `undefined`gets converted to `NaN`and `NaN`is a special numeric value which returns`false` for all comparisons.
-The equality check`(3)`returns `false` because `undefined`only equals`null`,`undefined`,and no other value.
-Porovnání `(1)`a `(2)`vracejí `false`, protože `undefined`se převede na `NaN`a `NaN`je speciální číselná hodnota, která při všech porovnáních vrátí`false`.
-Test rovnosti`(3)`vrací `false`, protože `undefined`se rovná jedině`null` a`undefined`,žádné jiné hodnotě.
###Avoid problems
###Jak se vyhnout problémům
Why did we go over these examples? Should we remember these peculiarities all the time? Well, not really. Actually, these tricky things will gradually become familiar over time, but there's a solid way to avoid problems with them:
Jak se přes tyto příklady přenést? Měli bychom si tyto zvláštnosti neustále pamatovat? Vlastně ani ne. Ve skutečnosti se s těmito triky postupem času sžijete, ale existuje solidní způsob, jak se vyhnout problémům, které způsobují:
-Treat any comparison with`undefined/null`except the strict equality`===`with exceptional care.
-Don't use comparisons`>= > < <=`with a variable which may be `null/undefined`,unless you're really sure of what you're doing. If a variable can have these values, check for them separately.
-Zacházejte s každým porovnáním s`undefined/null`kromě striktní rovnosti`===`výjimečně opatrně.
-Nepoužívejte porovnání`>= > < <=`na proměnnou, která by mohla být `null/undefined`,pokud si opravdu nejste jisti tím, co děláte. Může-li proměnná obsahovat tyto hodnoty, ověřte je zvlášť.
##Summary
##Shrnutí
-Comparison operators return a boolean value.
-Strings are compared letter-by-letter in the "dictionary" order.
-When values of different types are compared, they get converted to numbers (with the exclusion of a strict equality check).
-The values`null`and `undefined`equal`==`each other and do not equal any other value.
-Be careful when using comparisons like `>` or `<` with variables that can occasionally be`null/undefined`.Checking for `null/undefined` separately is a good idea.
-Porovnávací operátory vracejí hodnotu typu boolean.
-Řetězce se porovnávají znak po znaku podle „slovníkového“ pořadí.
-Když se porovnávají hodnoty různých typů, převedou se na čísla (s výjimkou operátoru striktní rovnosti).
-Hodnoty`null`a `undefined`se rovnají`==`sobě navzájem, ale nerovnají se žádné jiné hodnotě.
-Buďte opatrní při používání porovnávání jako `<` nebo `>` na proměnné, které mohou být`null/undefined`.Dobrý nápad je odděleně ověřit, zda opravdu jsou `null/undefined`.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.