30 Days Javascript Challenge (by Hitesh Choudhary) Day 1 - Variables and Data Types

Alpit KumarAlpit Kumar
3 min read

First you can visit to the Assignment or I put the screenshot of same here:
https://courses.chaicode.com/learn/home/30-days-of-Javascript-challenge/30-days-javascript-challenge/section/515627/lesson/3196994

Task 1:

var is used for declaring a variable or we can say that the value is being assign to the variable.

Example:

var myNumber = 3;
console.log(myNumber); // Output: 3

Task 2:

let is also used to declare a variable, same as of var.
The difference between let and var is in the scope of the variables they create: Variables declared by let are only available inside the block where they're defined. Variables declared by var are available throughout the function in which they're declared.

Example:

let myName = "Alpit";
console.log(myName); // Output: Alpit

Task 3:

When we use const for a declaration of a variable, we can't assign another value, as it is possible in let and var.

Example:

const codemore = "true";
console.log(codemore); // Output: true

Task 4:

Mainly there are total 5 Data types in Javascript.

Example:

let num = 4;
let str = "Javascript 30 Days Challenge";
let checking = "false";
let object = { name: "Alpit", age: 20 };
let arr = [1, 2, 3, 4, 5];

console.log(typeof num);      // Output: number
console.log(typeof str);      // Output: string
console.log(typeof checking); // Output: string
console.log(typeof object);   // Output: object
console.log(typeof arr);      // Output: object

Task 5:

When we redeclare the variable which was initially defined using let, it's value can be changed later.

Example:

let myVariable = 10;
console.log(myVariable); // Output: 10

myVariable = "Hello World";
console.log(myVariable); // Output: Hello World

Task 6:

When we reassign the value to the const variable, it gives the error.

Example:

const myConstant = 100;
console.log(myConstant); // Output: 100

// Trying to reassign will cause an error
myConstant = 200;
console.log(myConstant); // Uncaught TypeError: Assignment to constant variable.

Feature Request

1. Variable Types Console Log

Example:

let numberVar = 25;
let stringVar = "This is a string";
let booleanVar = true;
let objectVar = { key: "value" };
let arrayVar = [1, 2, 3];

console.log(`Value: ${numberVar}, Type: ${typeof numberVar}`); // Output: Value: 25, Type: number
console.log(`Value: ${stringVar}, Type: ${typeof stringVar}`); // Output: Value: This is a string, Type: string
console.log(`Value: ${booleanVar}, Type: ${typeof booleanVar}`); // Output: Value: true, Type: boolean
console.log(`Value: ${objectVar}, Type: ${typeof objectVar}`); // Output: Value: [object Object], Type: object
console.log(`Value: ${arrayVar}, Type: ${typeof arrayVar}`); // Output: Value: 1,2,3, Type: object

2. Reassignment Demo:

Example:

let reassignableVar = "Initial value";
console.log(reassignableVar); // Output: Initial value

reassignableVar = "New value";
console.log(reassignableVar); // Output: New value

const constantVar = "Fixed value";
console.log(constantVar); // Output: Fixed value

// Trying to reassign will cause an error
constantVar = "Attempt to change"; // Uncaught TypeError: Assignment to constant variable.
0
Subscribe to my newsletter

Read articles from Alpit Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Alpit Kumar
Alpit Kumar

I am a passionate web developer and open-source enthusiast on a captivating journey of coding wonders. With a year of experience in web development, my curiosity led me to the enchanting world of React, where I found a true calling. Embracing the magic of collaboration and knowledge-sharing, I ventured into the realm of open source, contributing to Digital Public Goods (DPGs) for the betterment of the digital universe. A firm believer in learning in public, I share my insights and discoveries through blogging, inspiring fellow coders to embark on their own magical coding odysseys. Join me on this thrilling adventure, where imagination and technology converge, and together, let's shape the future of the digital landscape! 🎩✨