◐ Shell
clean mode source ↗

Strings by mahdiHash · Pull Request #128 · javascript-tutorial/fa.javascript.info

@@ -1,19 +1,19 @@ We can't "replace" the first character, because strings in JavaScript are immutable. ما نمی‌توانیم حرف اول را «جایگزین» کنیم، چون رشته‌ها در جاوااسکریپت غیر قابل تغییر هستند.
But we can make a new string based on the existing one, with the uppercased first character: اما می‌توانیم یک رشته جدید را بر اساس رشته موجود با کاراکتر اول بزرگ شده بسازیم:
```js let newStr = str[0].toUpperCase() + str.slice(1); ```
There's a small problem though. If `str` is empty, then `str[0]` is `undefined`, and as `undefined` doesn't have the `toUpperCase()` method, we'll get an error. البته یک مشکل کوچک وجود دارد. اگر `str` خالی باشد، پس `str[0]` برابر با `undefined` است و `undefined` متد `toUpperCase()` را ندارد، پس ما ارور خواهیم داشت.
There are two variants here: دو روش پیش روی‌مان داریم:
1. Use `str.charAt(0)`, as it always returns a string (maybe empty). 2. Add a test for an empty string. 1. از `str.charAt(0)` استفاده کنیم، چون همیشه یک رشته برمی‌گرداند (شاید رشته خالی). 2. یک تست برای رشته خالی اضافه کنیم.
Here's the 2nd variant: روش دوم را اینجا داریم:
```js run demo function ucFirst(str) { Expand Down