diff --git a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md index 7dd0d61c2..3bee4e939 100644 --- a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md +++ b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/solution.md @@ -16,9 +16,9 @@ null + 1 = 1 // (5) undefined + 1 = NaN // (6) ``` -1. The addition with a string `"" + 1` converts `1` to a string: `"" + 1 = "1"`, and then we have `"1" + 0`, the same rule is applied. -2. The subtraction `-` (like most math operations) only works with numbers, it converts an empty string `""` to `0`. -3. The addition with a string appends the number `5` to the string. -4. The subtraction always converts to numbers, so it makes `" -9 "` a number `-9` (ignoring spaces around it). -5. `null` becomes `0` after the numeric conversion. -6. `undefined` becomes `NaN` after the numeric conversion. +1. Η πρόσθεση με μια συμβολοσειρά όπως π.χ. `"" + 1`, μετατρέπει το `1` σε μια συμβολοσειρά: `"" + 1 = "1"`, και τότε έχουμε `"1" + 0`, όπου ο ίδιος κανόνας εφαρμόζεται. +2. Η αφαίρεση `-` (όπως στις περισσότερες Μαθηματικές πράξεις) λειτουργεί μόνο με αριθμούς, και μετατρέπει μια άδεια συμβολοσειρά `""` σε `0`. +3. Η πρόσθεση με μια συμβολοσειρά, τοποθετεί τον αριθμό `5` στη συμβολοσειρά (επιπλέον παράδειγμα: `5 +"9" = "59"`). +4. Η αφαίρεση μετατρέπει τις τιμές πάντα σε αριθμούς, επομένως εδώ μετατρέπει το `" -9 "` στον αριθμό `-9` (αγνοώντας τα κενά γύρω του). +5. Η `null` γίνεται `0` μετά την αριθμητική μετατροπή. +6. Η `undefined` γίνεται `NaN` μετά την αριθμητική μετατροπή. diff --git a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md index f17e870de..f57ec6c03 100644 --- a/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md +++ b/1-js/02-first-steps/06-type-conversions/1-primitive-conversions-questions/task.md @@ -2,9 +2,9 @@ importance: 5 --- -# Type conversions +# Μετατροπές τύπων -What are results of these expressions? +Ποια είναι τα αποτελέσματα αυτών των εκφράσεων; ```js no-beautify "" + 1 + 0 @@ -23,4 +23,4 @@ null + 1 undefined + 1 ``` -Think well, write down and then compare with the answer. +Σκεφτείτε καλά, γράψτε κάτω τις απαντήσεις σας και μετά συγκρίνετε τες με την απάντηση. diff --git a/1-js/02-first-steps/06-type-conversions/article.md b/1-js/02-first-steps/06-type-conversions/article.md index 6ac695e84..ccb8961d6 100644 --- a/1-js/02-first-steps/06-type-conversions/article.md +++ b/1-js/02-first-steps/06-type-conversions/article.md @@ -1,160 +1,152 @@ -# Type Conversions +# Μετατροπές Τύπων -Most of the time, operators and functions automatically convert the values given to them to the right type. +Τις περισσότερες φορές, οι τελεστές και οι συναρτήσεις μετατρέπουν αυτόματα, και στον σωστό τύπο, τις τιμές που δίνονται σε αυτές. -For example, `alert` automatically converts any value to a string to show it. Mathematical operations convert values to numbers. +Για παράδειγμα, η `alert` μετατρέπει αυτόματα σε string οποιαδήποτε τιμή, ώστε να την εμφανίσει. Οι Μαθηματικές πράξεις μετατρέπουν τιμές σε νούμερα. -There are also cases when we need to explicitly convert a value to the expected type. +Υπάρχουν επίσης περιπτώσεις όπου χρειαζόμαστε να μετατρέψουμε ρητά μια τιμή στον προσδοκώμενο τύπο. -```smart header="Not talking about objects yet" -In this chapter, we won't cover objects. Instead, we'll study primitives first. Later, after we learn about objects, we'll see how object conversion works in the chapter . +```smart header="Δεν μιλάμε ακόμα για αντικείμενα" +Σε αυτό το κεφάλαιο, δεν θα καλύψουμε την ύλη για τα αντικείμενα. Αντίθετα, θα μελετήσουμε πρώτα τις απλές τιμές. Αργότερα, όταν μάθουμε για τα αντικείμενα, θα δούμε πώς δουλεύει η μετατροπή των αντικειμένων στο κεφάλαιο . ``` ## ToString -String conversion happens when we need the string form of a value. +Η μετατροπή συμβολοσειρών γίνεται όταν χρειαζόμαστε μια τιμή σε μορφή συμβολοσειράς. -For example, `alert(value)` does it to show the value. +Για παράδειγμα, η `alert(value)` κάνει αυτή τη μετατροπή, ώστε να εμφανίσει την τιμή. -We can also call the `String(value)` function to convert a value to a string: +Μπορούμε επίσης να καλέσουμε τη συνάρτηση `String(value)`, για να μετατρέψουμε μια τιμή σε μια συμβολοσειρά: ```js run let value = true; alert(typeof value); // boolean *!* -value = String(value); // now value is a string "true" +value = String(value); // τώρα η value είναι η συμβολοσειρά "true" alert(typeof value); // string */!* ``` -String conversion is mostly obvious. A `false` becomes `"false"`, `null` becomes `"null"`, etc. +Η μετατροπή συμβολοσειρών είναι αρκετά προφανής. Η `false` γίνεται `"false"`, η `null` γίνεται `"null"`, κλπ. ## ToNumber -Numeric conversion happens in mathematical functions and expressions automatically. +Η μετατροπή αριθμών γίνεται αυτόματα σε Μαθηματικές συναρτήσεις και εκφράσεις. -For example, when division `/` is applied to non-numbers: +Για παράδειγμα, όταν η διαίρεση `/` εφαρμόζεται σε μη αριθμούς: ```js run -alert( "6" / "2" ); // 3, strings are converted to numbers +alert("6" / "2"); // 3, οι συμβολοσειρές μετατρέπονται σε αριθμούς ``` -We can use the `Number(value)` function to explicitly convert a `value` to a number: +Μπορούμε να χρησιμοποιήσουμε τη συνάρτηση `Number(value)` για τη ρητή μετατροπή της `value` σε έναν αριθμό: ```js run let str = "123"; alert(typeof str); // string -let num = Number(str); // becomes a number 123 +let num = Number(str); // γίνεται ο αριθμός 123 alert(typeof num); // number ``` -Explicit conversion is usually required when we read a value from a string-based source like a text form but expect a number to be entered. +Η ρητή μετατροπή συνήθως απαιτείται όταν διαβάζουμε μια τιμή από μια πηγή με βάση τις συμβολοσειρές, όπως μια φόρμα κειμένου, αλλά περιμένουμε την εισαγωγή ενός αριθμού. -If the string is not a valid number, the result of such a conversion is `NaN`. For instance: +Εάν η συμβολοσειρά δεν περιέχει έναν έγκυρο αριθμό, τότε το αποτέλεσμα μιας τέτοιας μετατροπής είναι `NaN`. Για παράδειγμα: ```js run -let age = Number("an arbitrary string instead of a number"); +let age = Number("μια αυθαίρετη συμβολοσειρά αντί ενός αριθμού"); -alert(age); // NaN, conversion failed +alert(age); // NaN, η μετατροπή απέτυχε ``` -Numeric conversion rules: +Κανόνες αριθμητικών μετατροπών: -| Value | Becomes... | -|-------|-------------| -|`undefined`|`NaN`| -|`null`|`0`| -|true and false | `1` and `0` | -| `string` | Whitespaces from the start and end are removed. If the remaining string is empty, the result is `0`. Otherwise, the number is "read" from the string. An error gives `NaN`. | +| Τιμή | Γίνεται... | +| ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `undefined` | `NaN` | +| `null` | `0` | +| true και false | `1` και `0` | +| `string` | Οι κενοί χαρακτήρες από την αρχή και το τέλος αφαιρούνται. Εάν το υπολοιπόμενο περιεχόμενο της συμβολοσειράς είναι άδειο, τότε το αποτέλεσμα είναι `0`. Διαφορετικά, ο αριθμός "διαβάζεται" από τη συμβολοσειρά. Ένα σφάλμα δίνει `NaN`. | -Examples: +Παραδείγματα: ```js run -alert( Number(" 123 ") ); // 123 -alert( Number("123z") ); // NaN (error reading a number at "z") -alert( Number(true) ); // 1 -alert( Number(false) ); // 0 +alert(Number(" 123 ")); // 123 +alert(Number("123ω")); // NaN (σφάλμα όταν προσπαθεί να διαβάσει αριθμό στο "ω") +alert(Number(true)); // 1 +alert(Number(false)); // 0 ``` -Please note that `null` and `undefined` behave differently here: `null` becomes zero while `undefined` becomes `NaN`. +Παρακαλώ προσέξτε ότι οι τιμές `null` και `undefined` συμπεριφέρονται διαφορετικά εδώ: η `null` γίνεται 0 (μηδέν), ενώ η `undefined` γίνεται `NaN`. -````smart header="Addition '+' concatenates strings" -Almost all mathematical operations convert values to numbers. A notable exception is addition `+`. If one of the added values is a string, the other one is also converted to a string. +````smart header="Ο τελεστής '+' συνενώνει συμβολοσειρές" +Σχεδόν όλες οι Μαθηματικές πράξεις μετατρέπουν τιμές σε αριθμούς. Μια ξεχωριστή εξαίρεση είναι ο τελεστής `+`. Έαν μία από τις προστιθέμενες τιμές είναι μια συμβολοσειρά, τότε και η άλλη μετατρέπεται σε συμβολοσειρά. -Then, it concatenates (joins) them: +Τότε, τις συνενώνει: ```js run -alert( 1 + '2' ); // '12' (string to the right) -alert( '1' + 2 ); // '12' (string to the left) +alert( 1 + '2' ); // '12' (συμβολοσειρά στα δεξιά) +alert( '1' + 2 ); // '12' (συμβολοσειρά στα αριστερά) ``` -This only happens when at least one of the arguments is a string. Otherwise, values are converted to numbers. +Αυτό συμβαίνει μόνο όταν τουλάχιστον μία από τις παραμέτρους είναι μια συμβολοσειρά. Διαφορετικά, οι τιμές μετατρέπονται σε αριθμούς. ```` ## ToBoolean -Boolean conversion is the simplest one. +Η μετατροπή αληθοτιμών είναι η πιο απλή. -It happens in logical operations (later we'll meet condition tests and other similar things) but can also be performed explicitly with a call to `Boolean(value)`. +Γίνεται σε λογικές πράξεις (αργότερα θα συναντήσουμε ελέγχους συνθηκών και άλλα παρόμοια), αλλά μπορεί επίσης να γίνει ρητά με μια κλήση της `Boolean(value)`. -The conversion rule: +Ο κανόνας μετατροπής: -- Values that are intuitively "empty", like `0`, an empty string, `null`, `undefined`, and `NaN`, become `false`. -- Other values become `true`. +- Τιμές που είναι διαισθητικά "άδειες", όπως το `0`, μια άδεια συμβολοσειρά, η `null`, η `undefined`, και η `NaN`, γίνονται `false`. +- Άλλες τιμές γίνονται `true`. -For instance: +Για παράδειγμα: ```js run -alert( Boolean(1) ); // true -alert( Boolean(0) ); // false +alert(Boolean(1)); // true +alert(Boolean(0)); // false -alert( Boolean("hello") ); // true -alert( Boolean("") ); // false +alert(Boolean("γεια")); // true +alert(Boolean("")); // false ``` -````warn header="Please note: the string with zero `\"0\"` is `true`" -Some languages (namely PHP) treat `"0"` as `false`. But in JavaScript, a non-empty string is always `true`. - -```js run -alert( Boolean("0") ); // true -alert( Boolean(" ") ); // spaces, also true (any non-empty string is true) -``` -```` -## Summary +## Περίληψη -The three most widely used type conversions are to string, to number, and to boolean. +Οι τρεις πιο διαδεδομένα χρησιμοποιούμενες μετατροπές τύπων, είναι: σε συμβολοσειρά, σε αριθμό, και σε αληθοτιμή. -**`ToString`** -- Occurs when we output something. Can be performed with `String(value)`. The conversion to string is usually obvious for primitive values. +**`ToString`** -- Γίνεται όταν κάτι το δίνουμε ως έξοδο. Μπορεί να εφαρμοστεί με τη `String(value)`. Η μετατροπή σε συμβολοσειρά είναι συνήθως προφανής για τις απλές τιμές. -**`ToNumber`** -- Occurs in math operations. Can be performed with `Number(value)`. +**`ToNumber`** -- Γίνεται στις Μαθηματικές πράξεις. Μπορεί να εφαρμοστεί με τη `Number(value)`. -The conversion follows the rules: +Η μετατροπή ακολουθεί τους κανόνες: -| Value | Becomes... | +| Τιμή | Γίνεται... | |-------|-------------| |`undefined`|`NaN`| |`null`|`0`| |true / false | `1 / 0` | -| `string` | The string is read "as is", whitespaces from both sides are ignored. An empty string becomes `0`. An error gives `NaN`. | +| `string` | Η συμβολοσειρά διαβάζεται "όπως είναι", τα κενά και από τις δύο πλευρές αγνοούνται. Μια άδεια συμβολοσειρά γίνεται `0`. Ένα σφάλμα δίνει `NaN`. | -**`ToBoolean`** -- Occurs in logical operations. Can be performed with `Boolean(value)`. +**`ToBoolean`** -- Γίνεται σε λογικές πράξεις. Μπορεί να εφαρμοστεί με τη `Boolean(value)`. -Follows the rules: +Ακολουθεί τους κανόνες: -| Value | Becomes... | +| Τιμή | Γίνεται ... | |-------|-------------| |`0`, `null`, `undefined`, `NaN`, `""` |`false`| -|any other value| `true` | +|κάθε άλλη τιμή| `true` | -Most of these rules are easy to understand and memorize. The notable exceptions where people usually make mistakes are: +Οι περισσότεροι από αυτούς τους κανόνες είναι εύκολοι να τους καταλάβεις και να τους θυμάσαι. Οι ξεχωριστές εξαιρέσεις που οι άνθρωποι συνήθως κάνουν λάθη, είναι: -- `undefined` is `NaN` as a number, not `0`. -- `"0"` and space-only strings like `" "` are true as a boolean. +- η `undefined` όταν θεωρείται σαν αριθμός, είναι `NaN` και όχι `0`. +- η `"0"` και οι συμβολοσειρές μόνο με κενά όπως `" "`, είναι true ως αληθοτιμές. -Objects aren't covered here. We'll return to them later in the chapter that is devoted exclusively to objects after we learn more basic things about JavaScript. +Η ύλη για τα αντικείμενα δεν καλύπτεται εδώ. Θα επιστρέψουμε αργότερα σε αυτά στο κεφάλαιο , το οποίο είναι αποκλειστικά αφιερωμένο στα αντικείμενα, δηλ. μετά που θα μάθουμε περισσότερες βασικές έννοιες για τη JavaScript.