0% found this document useful (0 votes)
7 views5 pages

JavaScript Type Conversion and Operators

The document explains the differences between type conversion and coercion in JavaScript, highlighting how operations like addition and subtraction behave differently with strings and numbers. It also covers the distinctions between the == and === operators, emphasizing the importance of data type in comparisons. Additionally, it discusses control flow with if-else statements and provides a mini challenge regarding negative numbers.

Uploaded by

ransly 21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

JavaScript Type Conversion and Operators

The document explains the differences between type conversion and coercion in JavaScript, highlighting how operations like addition and subtraction behave differently with strings and numbers. It also covers the distinctions between the == and === operators, emphasizing the importance of data type in comparisons. Additionally, it discusses control flow with if-else statements and provides a mini challenge regarding negative numbers.

Uploaded by

ransly 21
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Part I: Type Conversion and Coercion

1. What is the difference between type conversion and type coercion?

Answer: Type conversion is when we manually change a value to another data


type in JavaScript (for example, using Number(), String(), etc.).
Type coercion is when JavaScript automatically converts a value to another
type during an operation (for example, "10" + 5 becomes "105" because JS
converts the number 5 to a string).

2. Why does "10" + 5 give a different result from "10" - 5?

Answer: 10 + 5 results in "105" because the + operator with a string causes


string concatenation. JavaScript coerces the number 5 into a string. While 10 -
5 results in 5 because the - operator forces JavaScript to treat "10" as a number
and perform subtraction.
3. What data type is returned by Number(num)?
Answer: Number(num) returns a number data type.

Part II: Operators in JavaScript

Answer the following:

1. What’s the difference between == and ===?

Answer: == checks if two values are the same, but it does not care about the
data type.
=== checks if the values are the same and if they have the same data type.
For example: 5 == "5" → true same value
5 === "5" false different type: number vs string

2. Which operator checks for both value and data type equality?

Answer: The === operator checks for both value and type equality.

3. Give one real-life example where comparison operators could be useful (e.g.,
checking user age).

Answer: You can use comparison operators to check if a person is old enough to
drive.

Part III: Control Flow


Answer the following:

1. What message appears when age is 17?

Answer: The message appears when age is 17 is You are not allowed to vote
yet.

2. What message appears when you change age to 20?


Answer: You are allowed to vote."

3. Explain what the if and else statements do in your own words.

Answer: The if statement checks a condition in this case, if the person’s age is
18 or older. If the condition is true, it runs the code inside the if part.
If the condition is false, it skips that part and runs the code inside the else part
instead.

Part IV: Mini Challenge


Question:
What happens if you change the value of number to -5?

Answer: When you change the value of number to -5, the program will show the
message The number is negative because -5 is less than zero.

You might also like