DEV Community

Guna Ramesh
Guna Ramesh

Posted on

Javascript Variables.

Task 1:Create Variables

Create 3 variables:

Your name
Your age
Your city

Use let for age, const for name
program:
let name="gunalan"
let age="23"
const city="pattukkottai"
console.log(name)
console.log(age)
console.log(city)

output:
gunalan
23

pattukkottai

Task 2: Update Value

let score = 10;
Change it to 50 and print it using:
console.log(score);

program:
let score=10;
score=50;
console.log(score)

output:

50

Task 3: Const Error Check

const country = "India";
Try to change it to "USA"
What happens? (observe error)

program:
const country="india";
country="usa";
console.log(country);

output:

TypeError: Assignment to constant variable.

Understanding Concepts

Task 4: Identify Output

program:
var x = 10;
var x = 20;
console.log(x);

output:

20

Task 5: Fix Error

let y = 30;
let y = 40;

Fix this code without error
Enter fullscreen mode Exit fullscreen mode

program:
let y=30;
y=40;
console.log(y)

output:

40

Task 6: Scope Test
{
let a = 100;
}
console.log(a);

Will it work or error? Why?

program:
{
let a = 100;
}
console.log(a);

output:
a is not defined . variable is inside a block but the print statement is outside
so couldn't print the output because let is a block-scope, if you did the print statement inside the

block it is printed.

Level 3: Real-Life Task (Your Project Style)
Task 7: Shopping Cart

Create variables:

const shopName = "RuralCart";
let items = 0;
let price = 100;

Do this:

Add 3 items → update items
Increase price → price = price + 50
Print all values

program:
const shopName = "RuralCart";
let items = 0;
let price = 100;
items=items+3;
price=price+50;
console.log(shopname)
console.log(items);
console.log(price);

output:
RuralCart
3

150

Bonus Challenge

Task 8: Combine Everything

Write a program:

productName
productPrice
quantity

Calculate total:

total = productPrice * quantity;

Print:

Product: Rice
Total Price: 500

program 1:

let productName="Rice";
let productPrice=50;
let quantity=10;
total=productPrice*quantity;
console.log(total)
Enter fullscreen mode Exit fullscreen mode

output:
500

program 2:
let productName="Rice";
let productPrice=50;
let quantity=10;
console.log(productPrice*quantity)

output:

500

Task 9: String + Variable

Create:

let name = "Guna";
let age = 23;

Print like this:

My name is Guna and I am 23 years old

program:
let name = "Guna";
let age = 23;
console.log("My name is "+name+" and I am "+age+" years old")

output:

**My name is Guna and I am 23 years old**
Enter fullscreen mode Exit fullscreen mode

Task 10: Total Price (Clean Output)
let product = "Sugar";
let price = 40;
let qty = 5;

expect Output:

Product: Sugar
Total: 200

program:
let product = "Sugar";
let price = 40;
let qty = 5;
let total=price*qty
console.log("Product:"+product)
console.log("Total:"+total)
output:
Product: Sugar

Total: 200

Top comments (0)