Type Conversion and Type Coercion

Wa maxey Type Conversion and Type Coercion? Micnahoodu waxa weeyo badalida data-type ka variable ka, sida 123 data-type keda oo ahaa Number inaad u badasho String.

Waxa jiro labo qaab oo aad u badali karto JavaScript variable data-type kisa. Marki aad adiga gacantada ku badasho waxa lo yaqana Type Conversion.

Example/Tusaale
				
					const num = "123";
console.log(typeof num); // data-type = string
const convertToNumber = Number(num);
console.log(typeof convertToNumber); // data-type = number
				
			

Tusaalahan kore waxaan badalney num variable oo data-type kiisu uu ahaa string waxana u badalney number, anagoo isticmaaleyno Number method.

Number method waxan u pass/gudbiney num variable kadib wuxu u badaley data-type kisa number.

Strings markan kaso hadleyney waxaan sheegney in waxkasto oo quotation marks ( “” ) la dhex galiyo in javaScript ey u arkeyso string.

Marki JavaScript ay si automatic ah u badasho variable data-type kisa waxa lo yaqaana Type Coercion. Automatic waxan ula jeeda in javaScript ey badasho variable data-type kiisa.

Example/Tusaale
				
					const age = 24;
console.log("da'daadu waa " + age);

				
			

tusaalahan kore qoraalkan “Da’daadu waa “+age markan so saareyney plus (+) operator-ka kahoreeyo variable age aya sababey in si automatic ah loo badalo type-ka age, waana mesha uu ka dhacayo Type Coercion.

Aqriste inaad fahanto Type Conversion and Type Coercion wa wax aad u muhiim ah waayo javaScript Type Coercion marar badan ayey sameysa.

Next Lesson >

Share