Kala duwanaanshaha ee JavaScript
Kala duwanaanshaha ama variables
waa meel lagu kaydiyo xogta si loo isticmaalo loona maamulo inta barnaamijka lagu jiro. JavaScript waxay leedahay saddex erey oo muhiim ah oo loo isticmaalo in lagu dhawaaqo kala duwanaanshaha: var
, let
, iyo const
.
Variables and Data Types in JavaScript |
var:
- Ereyga
var
waxaa loo isticmaalaa in lagu dhawaaqo kala duwanaanshaha ee JavaScript. Kala duwanaanshaha ku dhawaaqayvar
waxaa lagu soo koobay hawsha, taas oo macnaheedu yahay in ay kaliya laga heli karo hawshaas dhexdeeda. - Tusaale:javascript
var da' = 25; console.log(da'); // Wax soo saar: 25
- Ereyga
let:
- Ereyga
let
waxaa loo isticmaalaa in lagu dhawaaqo kala duwanaanshaha ee lagu soo koobay block-ga, taas oo macnaheedu yahay in ay kaliya laga heli karo block-kaas dhexdiisa. Tani waxay ka dhigaysaalet
mid ku habboon in loo isticmaalo loops iyo xaaladaha shuruudaha. - Tusaale:javascript
let magac = "John"; if (true) { let magac = "Doe"; console.log(magac); // Wax soo saar: Doe } console.log(magac); // Wax soo saar: John
- Ereyga
const:
- Ereyga
const
waxaa loo isticmaalaa in lagu dhawaaqo kala duwanaanshaha aan dib loo cusboonaysiin karin ka dib markii la isticmaalay. Kala duwanaanshahaconst
sidoo kale waa block-ku-soo-koobay sidalet
. Waxaa badanaa loo isticmaalaa qiimaha aan is beddelin inta barnaamijka la fulinayo. - Tusaale:javascript
const pi = 3.14; console.log(pi); // Wax soo saar: 3.14
- Ereyga
Noocyada Xogta ee JavaScript
JavaScript waxay taageertaa noocyo kala duwan oo xogta ah si loo kaydiyo noocyo kala duwan oo qiime ah. Noocyada xogta ugu muhiimsan waa:
String:
- Waxaa loo isticmaalaa in lagu matalo xogta qoraalka. Strings waxaa lagu dhex xiraa hal-fallaadh (
'
), labanlaab-fallaadh ("
), ama backticks (`
). - Tusaale:javascript
let salaanta = "Hello, World!"; console.log(salaanta); // Wax soo saar: Hello, World!
- Waxaa loo isticmaalaa in lagu matalo xogta qoraalka. Strings waxaa lagu dhex xiraa hal-fallaadh (
Number:
- Waxaa loo isticmaalaa in lagu matalo nambarada integer-ka iyo kuwa floating-point.
- Tusaale:javascript
let da' = 30; let qiimo = 19.99; console.log(da'); // Wax soo saar: 30 console.log(qiimo); // Wax soo saar: 19.99
Boolean:
- Waxay matalaysaa sheyga loojiga waxayna yeelan kartaa laba qiime:
true
amafalse
. - Tusaale:javascript
let waaLaGaliYey = true; let waaMaamul = false; console.log(waaLaGaliYey); // Wax soo saar: true console.log(waaMaamul); // Wax soo saar: false
- Waxay matalaysaa sheyga loojiga waxayna yeelan kartaa laba qiime:
Array:
- Waa ururin shey oo ku kaydsan hal variable. Arrays waa kuwa la nidaamiyay waxaana la tixraacaa laga bilaabo eber.
- Tusaale:javascript
let midabada = ["casaan", "cagaar", "buluug"]; console.log(midabada[0]); // Wax soo saar: casaan
Object:
- Waa ururin lamaane-key. Objects waxaa loo isticmaalaa in lagu kaydiyo qaab-dhismeed xogta oo adag.
- Tusaale:javascript
let qof = { magacKowaad: "John", magacDambe: "Doe", da': 25 }; console.log(qof.magacKowaad); // Wax soo saar: John
Undefined iyo Null:
undefined
waxay tilmaamaysaa in variable la dhawaaqay laakiin aan la siinin qiime.null
waxay matalaysaa maqnaanshaha ulajeedada ah ee qiimaha sheyga.- Tusaale:javascript
let aanLadhexGalinin; let maqan = null; console.log(aanLadhexGalinin); // Wax soo saar: undefined console.log(maqan); // Wax soo saar: null
Adigoo si wanaagsan u fahmaya kuna isticmaalaya kala duwanaanshaha iyo noocyada xogta si haboon, waxaad abuuri kartaa barnaamijyo JavaScript ah oo firfircoon oo dabacsan.
Tags
Learn javascript