JavaScript variables and data types

Exercise 1: Declaring Variables

Problem: Create three variables: a string named city, a number named population, and a boolean named isCapital. Assign appropriate values to them.

Explanation: This exercise helps understand the basic syntax for variable declaration in JavaScript using let or const. Variables are used to store data values.

Code:

let city = ‘New York’;

let population = 8419000;

let isCapital = false;

Exercise 2: Changing Variable Types

Problem: Declare a variable data and assign it a number. Then change its value to a string and log its type to the console.

Explanation: JavaScript variables are dynamically typed, meaning they can hold values of any type without any type enforcement.

Code:

let data = 42;

data = ‘Now I am a string’;

console.log(typeof data); // Should print ‘string’

Exercise 3: Understanding Const

Problem: Declare a constant PI and assign it the value of 3.14159. Try to reassign a new value to PI and observe what happens.

Explanation: const is used for declaring constants which are not supposed to change after their initial assignment.

Code:

const PI = 3.14159;

// PI = 3.14; // This line should cause an error when uncommented

Exercise 4: Working with Arrays

Problem: Create an array named colors containing three strings representing colors. Access the second item in the array and print it to the console.

Explanation: Arrays are used to store multiple values in a single variable and are accessed using index numbers.

Code:

let colors = [‘red’, ‘green’, ‘blue’];

console.log(colors[1]); // Accesses and prints ‘green’

Exercise 5: Object Properties

Problem: Create an object named car with properties make, model, and year. Log the model to the console.

Explanation: Objects are collections of properties, where each property is a key-value pair.

Code:

let car = {

 make: ‘Toyota’,

 model: ‘Corolla’,

 year: 2020

};

console.log(car.model); // Accesses and prints ‘Corolla’

Exercise 6: Using Null

Problem: Declare a variable unfinished and initialize it with null. Explain the significance of the value null in JavaScript.

Explanation: null in JavaScript is used to represent the intentional absence of any object value.

Code:

let unfinished = null;

// null is used to signify that ‘unfinished’ intentionally has no value

Exercise 7: Understanding Undefined

Problem: Declare a variable box. Log its value to the console without initializing it.

Explanation: Variables in JavaScript that are declared but not initialized have the value undefined.

Code:

let box;

console.log(box); // Should print ‘undefined’

Exercise 8: Concatenating Strings

Problem: Create two string variables, firstName and lastName. Combine them into a third variable fullName and print it.

Explanation: String concatenation is the process of joining two or more strings together.

Code:

let firstName = ‘John’;

let lastName = ‘Doe’;

let fullName = firstName + ‘ ‘ + lastName;

console.log(fullName);

Exercise 9: String and Number Interaction

Problem: Create a variable basePrice as a number and a variable taxRate as a decimal. Compute the total price and convert it to a string with a dollar sign in front.

Explanation: This exercise demonstrates type coercion between numbers and strings as well as basic arithmetic operations.

Code:

let basePrice = 50;

let taxRate = 0.05;

let totalPrice = basePrice * (1 + taxRate);

let displayPrice = ‘$’ + totalPrice.toFixed(2);

console.log(displayPrice);

Exercise 10: Boolean Logic

Problem: Create two boolean variables, isAdult and hasConsent. Create a new variable canProceed that checks if either condition is true.

Explanation: This exercise helps understand logical operators and boolean logic in JavaScript.

Code:

let isAdult = true;

let hasConsent = false;

let canProceed = isAdult || hasConsent;

console.log(canProceed); // Should print true