Загрузка...

Nullish coalescing in TypeScript | ?? operator tutorial | null, undefined and default value #kody_az

Nullish coalescing TypeScript-də ?? operatoru ilə yazılır və default dəyər vermək üçün istifadə olunur. Bu operator sol tərəfdəki dəyəri yoxlayır və əgər həmin dəyər null və ya undefined olarsa, sağ tərəfdəki dəyəri götürür. Əgər sol tərəfdə 0, false və ya boş string "" varsa, ?? onları dəyişmir, çünki bunlar real dəyərlər hesab olunur. Bu xüsusiyyətinə görə ?? operatoru çox vaxt || operatorundan daha təhlükəsizdir. Məsələn, məhsulun endirimi 0 ola bilərsə, discount ?? 10 yazmaq discount || 10 yazmaqdan daha doğrudur. Çünki 0 burada “endirim yoxdur” mənasını verən real dəyərdir və default dəyərlə əvəz olunmamalıdır. Nullish coalescing əsasən API-dən gələn məlumatlarda, istifadəçi formalarında, konfiqurasiya fayllarında və optional property-lərlə işləyərkən çox faydalıdır. O, optional chaining ?. ilə birlikdə də tez-tez istifadə olunur, məsələn user.profile?.city ?? "Şəhər məlum deyil". Bu kod o deməkdir ki, əgər istifadəçinin şəhər məlumatı yoxdursa, proqram xəta vermədən default mətn göstərəcək. Qısa desək, ?? operatoru yalnız həqiqətən dəyər olmayan hallarda, yəni null və undefined olduqda ehtiyat dəyər seçmək üçün istifadə olunur.

=====

Nullish coalescing in TypeScript is written with the ?? operator, and it is used to provide a default value. This operator checks the value on the left side, and if that value is null or undefined, it returns the value on the right side. If the left side contains 0, false, or an empty string "", the ?? operator does not replace them, because they are considered real values. Because of this, ?? is often safer than the || operator. For example, if a product discount can be 0, writing discount ?? 10 is better than writing discount || 10. That is because 0 can mean “no discount”, so it is a valid value and should not be replaced by a default value. Nullish coalescing is very useful when working with data from APIs, user forms, configuration files, and optional object properties. It is also often used together with optional chaining ?., for example user.profile?.city ?? "Unknown city". This code means that if the user’s city information does not exist, the program will show a default text without causing an error. In short, the ?? operator is used to choose a fallback value only when the original value is really missing, meaning it is null or undefined.

#kody_az #kodyaz #js #typescript #nodejs #es6 #ts #viral #viralcode #viralvideo #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses

Видео Nullish coalescing in TypeScript | ?? operator tutorial | null, undefined and default value #kody_az канала KODY•AZ
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять