Challenges
Variables

JavaScript Variables

Activity 1

Task 1: Create a Variable using var Keyword and log it to the console

Code:

var num = 10;
console.log(num); // 10

Outcome:

  • The console will display 10.
  • The variable num is declared using var and assigned the value 10.

Task 2: Create a Variable using let Keyword and log it to the console

Code:

let Str = "Hello Chai";
console.log(Str); // "Hello Chai"

Outcome:

  • The console will display Hello Chai.
  • The variable Str is declared using let and assigned the value "Hello Chai".

Activity 2

Task 3: Create a constant using const Keyword and log it to the console

Code:

const bool = true;
console.log(bool); // True

Outcome:

  • The console will display true.
  • The constant bool is declared using const and assigned the value true.

Activity 3

Task 4: Make variables with different data types and print their type to the console

Code:

let number = 10;
let string = "hello World";
let boolean = true;
let object = {
    name: "Chai",
    key: "code"
};
let array = ['Fruits', 'Chai', 'Vegetable'];
 
console.log(typeof number);  // number
console.log(typeof string);  // string
console.log(typeof boolean); // boolean
console.log(typeof object);  // object
console.log(typeof array);   // object (arrays are of type object in JavaScript)

Outcome:

  • The console will display:
    number
    string
    boolean
    object
    object
  • The types of the variables are printed using the typeof operator.

Activity 4

Task 5: Declare a variable using let Keyword and assign it a value and reassign its value

Code:

let chai = "code";
console.log(chai); // "code"
chai = "Hitesh Sir";
console.log(chai); // "Hitesh Sir"

Outcome:

  • The console will display:
    code
    Hitesh Sir
  • The variable chai is declared using let, initially assigned the value "code", and then reassigned the value "Hitesh Sir".

Activity 5

Task 6: Make a constant using const Keyword and try to reassign the value and Identify the error

Code:

const chaiCode = "ChaiCode.com";
console.log(chaiCode); // "ChaiCode.com"
chaiCode = "www.chaicode.com"; // This line will cause an error

Outcome:

  • The console will display:
    ChaiCode.com
  • Then, it will throw an error: TypeError: Assignment to constant variable.
  • The constant chaiCode is declared using const and assigned the value "ChaiCode.com". Attempting to reassign it results in an error since constants cannot be reassigned.

Feature Request 1

Code:

let number1 = 10;
let string1 = "hello World";
let boolean1 = true;
let object1 = {
    name: "Chai",
    key: "code"
};
let array1 = ['Fruits', 'Chai', 'Vegetable'];
 
console.log(`${number1} is a  ${typeof number1}`);
console.log(`${string1} is a  ${typeof string1}`);
console.log(`${boolean1} is a  ${typeof boolean1}`);
console.log(`${object1} is a  ${typeof object1}`);
console.log(`${array1} is a  ${typeof array1}`);

Outcome:

  • The console will display:
    10 is a  number
    hello World is a  string
    true is a  boolean
    [object Object] is a  object
    Fruits,Chai,Vegetable is a  object
  • This shows the type of each variable using template literals and typeof.

Feature Request 2

Code:

let hello = "world";
console.log(hello);
hello = "Hello Chai";
 
const HiteshSir = 'Chai Aur Code';
console.log(HiteshSir);
HiteshSir = 'www.chaicode.com'; // This line will cause an error

Outcome:

  • The console will display:
    world
    Hello Chai
    Chai Aur Code
  • Then, it will throw an error: TypeError: Assignment to constant variable.
  • The variable hello is declared using let, initially assigned the value "world", and then reassigned the value "Hello Chai".
  • The constant HiteshSir is declared using const and assigned the value 'Chai Aur Code'. Attempting to reassign it results in an error since constants cannot be reassigned.

Summary of Key Learnings:

  1. Variable Declarations:

    • var declares variables with function scope or globally if declared outside a function.
    • let declares variables with block scope, allowing reassignment.
    • const declares variables with block scope, but they cannot be reassigned after the initial assignment.
  2. Type Checking:

    • The typeof operator is used to determine the type of a variable.
  3. Reassignment:

    • Variables declared with let can be reassigned.
    • Constants declared with const cannot be reassigned, and attempting to do so results in an error.
  4. Template Literals:

    • Template literals (using backticks) allow embedding expressions within strings using ${expression}.

This CHallenge provides a comprehensive overview of JavaScript variables, covering variable declarations (var, let, const), type checking, reassignment rules, and usage examples. Each section includes code snippets, expected outcomes, and summaries of key learnings.