Type Conversion in JavaScript: Implicit and Explicit Type Conversions

 

Beddelka Nooca Xogta ee JavaScript: Beddelida Tooska ah iyo Midda Gacanta

JavaScript, beddelka nooca xogta waa habka lagu beddelayo qiimaha xogta laga bilaabo nooc xog ah ilaa nooc kale. Tani waa arrin caadi ah oo ku dhex jirta barnaamijyada, gaar ahaan marka aad la shaqeyso noocyo kala duwan oo xog ah sida xarfo, lambarro, iyo booliyiin. Beddelidda nooca xogta waxay noqon kartaa mid toos ah (implicit) ama mid gacanta lagu sameeyo (explicit). Fahamka beddelidahan waa muhiim si aad u qorto koodh nadiif ah oo khaladaad yar leh.

Type Conversion in JavaScript
Type Conversion in JavaScript


1. Beddelka Tooska ah ee Nooca Xogta (Implicit Type Conversion)

Beddelka tooska ah ee nooca xogta, oo sidoo kale loo yaqaan type coercion, waa marka JavaScript si toos ah u beddelo nooca xogta iyada oo aan barnaamij-sameeyaha faragelin. Tani waxay dhacdaa marka aad isku dayeyso inaad waxqabad ku samayso noocyo kala duwan oo xog ah. Tusaale ahaan, haddii aad isku dartid xaraf iyo nambar, JavaScript waxay si toos ah u beddeli doontaa nambarka si uu ugu shaqeeyo sidii xaraf.

Tusaale:

javascript

let num = 5; let str = "10"; let result = num + str; console.log(result); // 510

Sharaxaad: Halkan, num waa nambar (5), halka str uu yahay xaraf ("10"). Marka aad isku darto labadan qiime, JavaScript waxay si toos ah u beddeli doontaa num oo ka dhigaysa xaraf, taasoo keenaysa natiijada "510".

Tusaale kale oo caadi ah ayaa ah marka aad isku daydo inaad nambar ku dhex isticmaasho string iyadoo aad u isticmaasho ficil farriin ku qorista. JavaScript waxay si toos ah u beddeshaa lambarka nooca string ah marka ficilkan la sameynayo.

javascript

let num = 42; let message = "Qiimaha waa " + num; console.log(message); // "Qiimaha waa 42"

Sharaxaad: Halkan, num waa nambar (42), laakiin marka lagu daro farriinta string ah, JavaScript waxay si toos ah u beddeshaa lambarka num si uu ugu noqdo string.

2. Beddelka Gacanta Lagu Sameeyo ee Nooca Xogta (Explicit Type Conversion)

Beddelka gacanta lagu sameeyo ee nooca xogta, oo sidoo kale loo yaqaan type casting, waa marka barnaamij-sameeyaha si qasab ah u beddelo nooca xogta. Tani waxay ku dhacdaa marka aad si cad u sheegto in nooca xogta loo beddelo mid kale. JavaScript waxay bixisaa dhowr hab oo lagu sameeyo beddelid nooc xog ah sida Number(), String(), iyo Boolean().

Tusaale:

javascript

let numStr = "20"; let num = Number(numStr); console.log(num); // 20 console.log(typeof num); // number

Sharaxaad: Halkan, numStr waa xaraf ("20"). Waxaan isticmaalnay Number() si aan ugu beddelno nambar. Hadda, num waa nambar dhab ah oo leh qiime 20.

Waxaa jira noocyo kale oo gacanta lagu sameeyo sida String() oo u beddesha nooc kale sida lambarka ama boolean-ka ah si uu u noqdo xaraf.

javascript

let isAvailable = true; let availableStr = String(isAvailable); console.log(availableStr); // "true" console.log(typeof availableStr); // string

3. Khaladaadka Beddelka Nooca Xogta

Beddelka nooca xogta, gaar ahaan midka tooska ah, wuxuu keeni karaa khaladaad haddii aadan si fiican ugu fiirsan. Tusaale ahaan, isku darka xarfo iyo nambarro ayaa sababi kara natiijooyin aan la filayn.

Tusaale:

javascript

let value1 = "5"; let value2 = 10; let result = value1 * value2; console.log(result); // 50

Sharaxaad: Halkan, marka lagu daro xaraf iyo nambarro, JavaScript waxay isku dayi doontaa inay beddesho xarafka si uu u noqdo nambar. Laakiin haddii xarafka uu yahay mid aan loo beddeli karin nambar, waxaa dhici kara qalad ama natiijooyin aan la filayn.

Gunaanad

Fahamka beddelka nooca xogta ee JavaScript waa mid muhiim ah si loo hubiyo in barnaamijkaagu uu si sax ah u shaqeeyo. Inaad garato farqiga u dhexeeya beddelka tooska ah iyo midka gacanta lagu sameeyo waxay kaa caawin doontaa inaad ka fogaato khaladaadka caadiga ah ee ku dhici kara markaad la shaqeyneyso noocyo kala duwan oo xog ah. Had iyo jeer waxaa fiican in si cad loo beddelo nooca xogta haddii aad shaki ku jirto, si aad u hubiso in natiijadu tahay mid la saadaalin karo oo sax ah.

Previous Post Next Post