Type Coercion in JavaScript:
Type coercion is the automatic or implicit conversion of values from one data type
to another(such as string to number) by JavaScript.
Ex:
let x=5+"5"; //"55"
Practical Exercise:
Experiment with type coercion by performing operations between different data
types.(eg: number+ string, boolean+number) and observe the result.
Scenario
Imagine you are developing a user-friendly interface for a social networking site.
Users need a way to fill out their profile information, and once submitted, the
page dynamically displays their details in a well-designed card format. You will
use:
>HTML to create the form structure.
>CSS to style the form and display the user's information attractively.
>JavaScript to handle the form submission, validate inputs, and dynamically display
the user's information.
Requirements
HTML:
Create a form with fields for:
Name (text input)
Age (number input)
Favorite Hobby (dropdown menu)
Preferences (checkboxes for options like "Receive Newsletters" and "Enable
Notifications")
A submit button.
Add a section to display the submitted user profile dynamically.
CSS:
Style the form with a modern look.
Add a styled "User Profile Card" where the submitted data will be displayed.
JavaScript:
Handle form submission using an event listener.
Validate inputs (e.g., check if the name is not empty and age is a valid number).
Dynamically create and display a "User Profile Card" with the input data.
Practical Assignments and Exercises:
1. Create a script that checks if a number is positive, negative or zero.
2. Write a program to check if a number is even or odd using the ternary operator.
3. Create a program to display the name of the month based on the number(1-12).
4. Create a grading system where:
>Marks>=90:"A"
>Marks>=75: "B"
>Marks>=50:"C"
>Marks<50:"Fail"
5. Scenario: Develop a 'Simple Calculator" program that takes two numbers and an
operator(+,-,*,/) as input and displays the reuslt using a switch statement
Example Input:
num1=10, num2=5, operator="+"
output: Result:15