1.You are building a login system. Store username and password in variables and check whether both are filled.
Program
let username="gunalan";
let password="ramesh"
if(username!==""&&password!==""){
console.log("filled")
}
else{
console.log("Not filled")
}
output
filled
Explanation:
I created two variables named username and password using the let keyword and assigned values to them.
Then, I used an if condition to check whether both variables are filled or not.
"" means empty
!== "" means not empty (filled)
&& means both conditions must be true
So, the condition checks if both username and password are not empty.
If both are filled, it prints "filled".
Otherwise, it prints "Not filled".
So the output is "filled".
2.Create variables to store a product's name, price, and stock. Print a message like:
"iPhone costs ₹60000 and only 5 left in stock"
program
let name="iPhone"
let price=60000
let stock=5
console.log(name,"costs","₹"+price,"and only",stock,"left in stock")
output
"iPhone costs ₹60000 and only 5 left in stock"
Explanation:
I created three variables using let: name, price, and stock, and assigned values to them.
Then, I used console.log() to print a message using those variables.
The output will be:
"iPhone costs ₹ 60000 and only 5 left in stock"
name → product name
price → product price
stock → available quantity
console.log() is used to display the message by combining text and variables.
3.Store a student's marks in 3 subjects and calculate total.
let tamil=98
let english=100
let maths=100
let total=tamil+english+maths
console.log(total)
output
298
explanation
I created three variables: tamil, english, and maths, and assigned marks to them.
Then, I calculated the total using:
let total = tamil + english + maths;
Finally, I printed the total using console.log().
4.Swap two numbers without using a third variable.
program
let a=10;
let b=20
a=a+b
b=a-b
a=a-b
console.log(a,b)
output
20 10
Explanation
Add both → a = a + b a=10+20=30
Get original a → b = a - b b=a-b =>30-20=10
Get original b → a = a - b a=a-b =>30-10=20
5.Create a variable that stores whether a user is logged in or not and show a welcome message.
program
let loggedin=true
if(loggedin){
console.log("Your'e welcome!")
}
else{
console.log("login first")
}
output
Your'e welcome!
Top comments (0)