Type Conversion — தமிழ்

</>Coding Tamilan.← JavaScript course

JavaScript · Lesson 09 / 32 · தமிழ்

Type Conversion

கருத்தைப் புரிந்துகொள்ளுங்கள். Code-ஐ மாற்றி இயக்குங்கள். ஒரு சிறிய பயிற்சியால் கற்றதை உறுதி செய்யுங்கள்.

Detailed notesRunnable JavaScriptPractice + answer

மூலப் பாட விளக்கங்கள் தமிழில் உள்ளன. கூடுதல் concept, practice மற்றும் answer notes ஆங்கிலத்தில் உள்ளன.

01 / Understand

முக்கியக் கருத்து

Form values often arrive as strings. Convert deliberately before doing arithmetic, and check whether the conversion produced a valid number.

Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.

02 / Explore

விரிவான விளக்கங்கள்

Type Conversion என்னன்னு பார்ப்போம்

JavaScript-ல values ஒரு type-ல இருந்து இன்னொரு type-க்கு convert ஆகலாம். இது explicit-ஆ (நீயே பண்றது) அல்லது implicit-ஆ (JavaScript automatically பண்றது — type coercion) நடக்கும்.

Explicit Conversion

JavaScript reference fragmentCODE
// Number-க்கு convert
Number("42");        // 42
Number("");          // 0
Number("hello");     // NaN
Number(true);        // 1
Number(null);        // 0
Number(undefined);   // NaN

parseInt("42px");    // 42  — first non-digit-ல நிக்கும்
parseFloat("3.14em"); // 3.14

// String-க்கு convert
String(42);          // "42"
String(true);        // "true"
String(null);        // "null"
(255).toString(16);  // "ff" (hex)

// Boolean-க்கு convert
Boolean(1);          // true
Boolean(0);          // false

Implicit Coercion (Automatic)

JavaScript reference fragmentCODE
// String + anything = string concatenation
"5" + 3;       // "53"  ← number string-ஆ coerce ஆகும்
"5" + true;    // "5true"

// Arithmetic operators (+ தவிர) number-க்கு coerce பண்ணும்
"5" - 3;       // 2
"5" * "2";     // 10
true + true;   // 2    ← booleans 1-ஆ coerce ஆகும்

// Comparison coercion
"5" == 5;      // true  ← == coercion பண்ணும்
"5" === 5;     // false ← === coercion இல்ல
ExpressionResultஏன்?
"2" + 2"22"String + number → concatenation
"2" - 20Subtraction number-க்கு coerce பண்ணும்
true + 12true → 1
false + 11false → 0
null + 11null → 0
undefined + 1NaNundefined → NaN

Rule: Coercion-ஐ நம்புவதை விட explicit conversion use பண்றது better. Comparisons-ல === use பண்ணு — surprise type coercion வராது.

03 / Read & predict

முழுமையான உதாரணம்

Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.

index.js · complete exampleCODE
const input = "25";
const count = Number(input);
console.log(count + 5);
console.log(Number.isNaN(Number("hello")));
எதிர்பார்க்கும் முடிவைப் பாருங்கள்
30
true
04 / Experiment

மாற்றி இயக்கிப் பாருங்கள்

Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.

Open editor ↗

Editor load ஆனதும் code-ஐ ஏற்றி Run அழுத்துங்கள்.

OneCompiler என்ற இலவச வெளிப்புற சேவை code-ஐ இயக்குகிறது. Run செய்யும் code அங்கு அனுப்பப்படும். தனிப்பட்ட தகவல்கள் அல்லது secret keys உள்ளிட வேண்டாம்.

05 / Your turn

சிறிய பயிற்சி

Practice challenge

Replace input with "8". Compare Number(input) + 5 with input + 5.

விடை மற்றும் விளக்கம்

The numeric expression is 13; string concatenation produces "85".

தவிர்க்க வேண்டிய தவறு: Number("") is 0, so a successful numeric conversion alone does not prove that a required field was filled in.

அடுத்த பாடத்திற்குத் தயாரா?

  • Explain the example in your own words.
  • Complete the challenge before revealing its answer.
  • Describe the common mistake and how to avoid it.
Coding Tamilan · Learn by doing.