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. تأخذ القطعة من 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، لذا فإن Karakter رقم 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. · اضغط تشغيل لرؤية المخرجات هنا.