Strings · מחרוזות
Strings are text
- A string is text inside quotes:
"Ada"or'Ada'. - Join strings with
+— this is called concatenation.
מחרוזות הן טקסט
- מחרוזת היא טקסט בתוך סימני ציטוט:
"Ada"או'Ada'. - חבר מחרוזות בעזרת
+— תהליך זה נקרא הדבקה.
let first = "Ada";
console.log("Hi, " + first + "!"); // → Hi, Ada!
Mind the spaces
+joins text exactly. It never adds a space for you.- Put the space inside the quotes, where you want it to appear.
שימו לב למרווחים
+מחבר טקסט בדיוק. היא לעולם לא מוסיפה מרווח עבורך.- הכנס את המרווח בתוך סימני הציטוט, שם תרצה שהוא יופיע.
let pet = "Rex";
console.log("Good dog," + pet + "!"); // → Good dog,Rex! (no space)
console.log("Good dog, " + pet + "!"); // → Good dog, Rex!
Length and capitals
.lengthcounts the characters..toUpperCase()and.toLowerCase()return a copy in CAPITALS or small letters.
אורך ואותיות גדולות/קטנות
.lengthסופר את התווים..toUpperCase()ו-.toLowerCase()מחזירות העתק באותיות גדולות או קטנות.
console.log("hello".length); // → 5
console.log("hello".toUpperCase()); // → HELLO
Index and slice a string · אינדקס ופרוסת מחרוזת
Characters are numbered from 0. A slice takes from start up to — but not including — end. · תווים מסומנים מה-0. פירוק מחרוזת (slice) לוקח מ-start ועד — אך לא כולל — end.
Reach inside a string
- Each character has a position, counting from 0:
text[0]is the first one. .slice(start, end)takes the part fromstartup to — but not including —end.
גישה לתוך מחרוזת
- לכל תווה יש מיקום, הספירה מתחילה מ-0:
text[0]הוא הראשון. .slice(start, end)לוקח את החלק מstartועד — אך לא כולל —end.
let word = "JAVASCRIPT";
console.log(word[0]); // → J
console.log(word.slice(0, 4)); // → JAVA
Watch out
- Positions start at
0, so the 4th character isword[3]. - String methods return a new string — the original never changes.
שימו לב
- המיקומים מתחילים ב-
0, ולכן האות ה-4 היאword[3]. - שיטות על מחרוזת מחזירות מחרוזת חדשה — המקורית לעולם אינה משתנה.
Common mistakes
- Index from
0;s.lengthis a property (no()). - Strings cannot be changed in place.
טעויות נפוצות
- אינדקס מתחיל ב-
0;s.lengthהוא תכונה (אין לה()). - מחרוזות אינן ניתנות לשינוי בתוך המקום.
Now you try
- Build and inspect strings.
- Press Check answer to test it.
כעת תנסו בעצמכם
- בנייה ובדיקה של מחרוזות.
- לחץ על בדיקת תשובה כדי לבדוק אותה.
let name = "Sam". Use + to log Hello, then the name then !, so it reads Hello, Sam!. · let name = "Sam". השתמש ב-+ כדי להוציא פלט ל-Hello, , ולאחר מכן לשם, ולאחר מכן ל-!, כך שיתקבל Hello, Sam!.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Log how many characters are in the string computer using .length (it is 8). · הקלט כמה תווים יש במשפט computer באמצעות .length (זהו 8).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Log the string cat in CAPITALS using .toUpperCase() (it is CAT). · הקלט את המשפט cat באותיות גדולות באמצעות .toUpperCase() (זהו CAT).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
let word = "code". Log just its first letter using word[0] (it is c). · let word = "code". הקלט רק את האות הראשונה שלו באמצעות word[0] (זהי c).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.