Noocyada Xogta ee Asaliga ah ee JavaScript: Number, String, Boolean, Null, Undefined
JavaScript waa mid ka mid ah luuqadaha barnaamijyada ee loogu isticmaalka badan yahay horumarinta websaydhyada. Waa luuqad caan ku ah fududeynta iyo dabacsanaanta. Mid ka mid ah fikradaha muhiimka ah ee JavaScript waa fahamka noocyada xogta. JavaScript waxay leedahay laba nooc oo waaweyn oo xogta ah: noocyada asaliga ah (primitive) iyo kuwa shayga ah (object types). Noocyada xogta asaliga ah waa kuwa ugu aasaasiga ah waxaana ku jira Number, String, Boolean, Null, iyo Undefined. Maqaalkaan waxaan ku eegeynaa noocyada xogta asaliga ah ee faahfaahsan.
Primitive Data Types - Number, String, Boolean, Null, Undefined. |
1. Number
JavaScript, nooca Number wuxuu metelaa labada tiro ee integer-ka iyo floating-point-ka. Waxaa loo isticmaalaa nooc kasta oo tiro ah, ha ahaato mid togan ama taban, mid weyn ama mid yar, xataa qiimaha khaaska ah sida Infinity
iyo NaN
(Not a Number).
javascript
let integer = 42;
let float = 3.14;
let negative = -7;
let infinity = Infinity;
let notANumber = NaN;
JavaScript waxay bixisaa dhowr habab oo lagu shaqeeyo tirooyinka, sida parseInt()
, parseFloat()
, toFixed()
, iyo toString()
.
javascript
let str = "123.45";
let num1 = parseInt(str); // 123
let num2 = parseFloat(str); // 123.45
let num3 = 123.456.toFixed(2); // "123.46"
let num4 = (123).toString(); // "123"
2. String
Strings ee JavaScript waa safaf xarfo ah oo loo isticmaalo in lagu metelo qoraalka. Strings waxaa lagu abuurayaa in lagu go'doomiyo qoraalka isku xigta gudaha labo qodob (''), labo qodob ("") ama backticks (`
). Backticks waxaa loo isticmaalaa sheekooyinka template-ka, taas oo u oggolaaneysa isku dhexgalka muujinta.
javascript
let singleQuote = 'Hello, world!';
let doubleQuote = "JavaScript is fun!";
let backtick = `The sum of 2 and 3 is ${2 + 3}`;
Strings waxay leeyihiin habab iyo hantiyo badan, sida length
, toUpperCase()
, toLowerCase()
, charAt()
, includes()
, iyo slice()
.
javascript
let text = "JavaScript";
let length = text.length; // 10
let upper = text.toUpperCase(); // "JAVASCRIPT"
let lower = text.toLowerCase(); // "javascript"
let char = text.charAt(0); // "J"
let includes = text.includes("Script"); // true
let sliced = text.slice(0, 4); // "Java"
3. Boolean
Nooca xogta Boolean wuxuu leeyahay labo kaliya qiimood oo suuragal ah: true
ama false
. Badanaa waxaa loo isticmaalaa bayaanada shuruudaha si loo xakameeyo socodka barnaamijka.
javascript
let isJavaScriptFun = true;
let isTired = false;
Booleans waa muhiim u ah isbarbar dhigga iyo hawlgallada macquulka ah, badanaana waa natiijada hawlgalladaas.
javascript
let isGreater = 5 > 3; // true
let isEqual = (5 === 5); // true
let notEqual = (5 !== 3); // true
let andOperation = (true && false); // false
let orOperation = (true || false); // true
4. Null
Null
waa qiimo khaas ah oo ku jira JavaScript kaas oo metelaya maqnaanshaha ujeedka kasta oo qiime. Badanaa waxaa loo isticmaalaa in lagu muujiyo in varaayablka si cad loogu dejiyay in aanu lahayn wax qiimo ah.
javascript
let empty = null;
5. Undefined
Undefined
waa qiimo asaliga ah oo tilmaamaya in varaayablka aan la siin wax qiimo ah. Haddii varaayabl la shaaciyo laakiin aan la dejin, waxaa si otomaatig ah loogu sameeyaa qiimaha undefined
.
javascript
let notAssigned;
console.log(notAssigned); // undefined
Waxaa muhiim ah in la ogaado in null
iyo undefined
ay yihiin kala duwan yihiin. Halka null
uu yahay qiimo la dejiyay, undefined
waa nooc, waxaana loo isticmaalaa in lagu metelo varaayablka aan la dejin ama hanti aan jirin.
javascript
console.log(null == undefined); // true
console.log(null === undefined); // false
Gunaanad
Fahamka noocyada xogta asaliga ah ee JavaScript waa muhiim u ah qorista koodh wax ku ool ah. Numbers, strings, booleans, null, iyo undefined waa aasaaska barnaamij kasta oo JavaScript ah. Adigoo barta noocyadaan iyo hababkooda la xiriira, waxaad si wax ku ool ah u maamuli kartaa xogta oo aad gaari kartaa go'aanno xog ogaal ah barnaamijkaaga.
Marka aad si qoto dheer ugu sii dhex gasho JavaScript, waxaad la kulmi doontaa noocyo xog oo aad u adag iyo qaabab, laakiin in aad si wanaagsan ugu fahanto noocyadan asaliga ah had iyo jeer waa muhiim. Haddii aad ku sameyneyso hawlgallada xisaabeed, aad wax ka beddeleyso qoraalka, aad go'aanno gaareyso, ama aad la tacaaleyso varaayablada aan lahayn qiimo, noocyadan xogta asaliga ah ayaa bixiya aasaaska hawlahan oo dhan iyo in ka badan.