0% found this document useful (0 votes)
15 views

JavaScript Basics Overview

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

JavaScript Basics Overview

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JavaScript Basics Overview

1. Fundamentals of JavaScript:
Variables and Data Types: let, const, var, string, number, boolean, null, undefined, object, array.
Operators: Arithmetic (+, -, *, /), Comparison (==, ===, !=, !==, >, <), Logical (&&, ||, !).
Conditionals: if, else if, else, switch statement.
Loops:
for, while, do...while.
Advanced Loop: for...of (for arrays), for...in (for objects).

1. Fundamentals of JavaScript

Variables and Data Types

Variables: Used to store data that can be used later in the program. JavaScript has three ways to
declare variables:
`let`: Block-scoped variable, allows reassignment.
`const`: Block-scoped constant, cannot be reassigned.
`var`: Function-scoped variable, allows reassignment.

Data Types: JavaScript has different data types to store various kinds of information.
Primitive Types:
`string`: Represents textual data. Example: `"Hello, World!"`
`number`: Represents numbers, both integers and floats. Example: `42`, `3.14`
`boolean`: Represents logical values. `true` or `false`.
`null`: Represents an intentional absence of any value.
`undefined`: Represents a variable that has been declared but has not been assigned a
value.
Non-Primitive Types:
`object`: Stores collections of key-value pairs. Example: `{ name: "John", age: 30 }`
`array`: Stores ordered lists of items. Example: `[1, 2, 3]`

Operators

Arithmetic Operators: Used for basic mathematical operations.


`+` (Addition): `5 + 2` results in `7`.
`-` (Subtraction): `5 - 2` results in `3`.
`*` (Multiplication): `5 * 2` results in `10`.
`/` (Division): `5 / 2` results in `2.5`.

Comparison Operators: Compare values and return a boolean (`true` or `false`).


`==` (Equality): Checks if values are equal. Example: `5 == '5'` (true).

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/12
`===` (Strict Equality): Checks if values and types are equal. Example: `5 === '5'` (false).
`!=` (Inequality): Checks if values are not equal. Example: `5 != '6'` (true).
`!==` (Strict Inequality): Checks if values and types are not equal. Example: `5 !== '5'`
(true).
`>` (Greater Than): `5 > 3` results in `true`.
`<` (Less Than): `5 < 3` results in `false`.

Logical Operators: Used to combine conditions.


`&&` (AND): Returns `true` if both conditions are `true`. Example: `true && false` results in
`false`.
`||` (OR): Returns `true` if at least one condition is `true`. Example: `true || false` results
in `true`.
`!` (NOT): Reverses the boolean value. Example: `!true` results in `false`.

Conditionals

if: Executes a block of code if the condition is `true`.

javascript

if (x > 10) {
console.log("x is greater than 10");
}

else if: Provides an additional condition if the previous one is `false`.

javascript

if (x > 10) {
console.log("x is greater than 10");
} else if (x === 10) {
console.log("x is exactly 10");
}

else: Executes if all previous conditions are `false`.

javascript

if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is less than or equal to 10");
}

switch statement: Evaluates an expression and executes code based on the matching `case`.

javascript

let day = 3;
switch (day) {
case 1:
console.log("Monday");

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/12
break;
case 2:
console.log("Tuesday");
break;
default:
console.log("Other day");
}

Loops

for loop: Repeats a block of code a specific number of times.

javascript

for (let i = 0; i < 5; i++) {


console.log(i);
}

while loop: Repeats as long as the condition is `true`.

javascript

let i = 0;
while (i < 5) {
console.log(i);
i++;
}

do...while loop: Executes the block once before checking the condition.

javascript

let i = 0;
do {
console.log(i);
i++;
} while (i < 5);

Advanced Loop

for...of (for arrays): Iterates over the elements of an array.

javascript

const numbers = [1, 2, 3];


for (let number of numbers) {
console.log(number);
}

for...in (for objects): Iterates over the keys of an object.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/12
javascript

const person = { name: "Alice", age: 25 };


for (let key in person) {
console.log(key, person[key]);
}

This covers the core aspects of JavaScript fundamentals, perfect for building a strong foundation in
programming!

now make some questions for my improvement in JS coding and problem solving skills

Here are some questions and challenges to help improve your JavaScript coding and problem-solving
skills. They range from basic to intermediate, covering variables, data types, loops, conditionals, and
more.

1. Variables and Data Types

Question 1: Create a variable using `let` to store your name and another using `const` to store
your birth year. Change the value of the name variable but try changing the value of the birth
year. What happens?
Question 2: Write a program that stores three different data types (a string, a number, and a
boolean) in variables and prints their types using `typeof`.

2. Operators

Question 3: Write a program that takes two numbers as input and prints their sum, difference,
product, and quotient.
Question 4: Compare two variables using both `==` and `===`. Explain the difference between
the two comparisons through code examples.

3. Conditionals

Question 5: Write a program that checks if a number is positive, negative, or zero using
`if...else`.
Question 6: Create a `switch` statement that takes a day of the week as input (e.g., 1 for
Monday) and prints out the corresponding day name.
Question 7: Write a program that takes a user's age as input and outputs whether they are a
minor, an adult, or a senior citizen using `if`, `else if`, and `else`.

4. Loops

Question 8: Write a `for` loop that prints numbers from 1 to 10.


Question 9: Write a `while` loop that prints the even numbers between 1 and 20.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/12
Question 10: Create a `do...while` loop that asks the user for a password until they enter the
correct one.

5. Advanced Loop

Question 11: Write a `for...of` loop that iterates over an array of colors and prints each color.
Question 12: Write a `for...in` loop that iterates over the properties of an object and prints
both the property names and their values.
Question 13: Given an array of numbers, use a `for...of` loop to find the sum of all the
elements.

6. Arrays and Objects

Question 14: Write a program that stores five numbers in an array. Find and print the maximum
number using a loop.
Question 15: Create an object representing a car (with properties like `make`, `model`, `year`).
Write a function that takes an object as an argument and prints out all its properties.
Question 16: Write a function that takes an array of numbers as input and returns a new array
with each number squared.

7. Functions

Question 17: Write a function called `isEven` that takes a number as input and returns `true` if
it is even and `false` if it is odd.
Question 18: Write a function `calculateArea` that takes the radius of a circle as a parameter
and returns the area of the circle.
Question 19: Create a function `greet` that takes a name as a parameter and prints a
personalized greeting.

8. Practice Problems

Question 20: Write a program that checks if a given word is a palindrome (reads the same
forwards and backwards).
Question 21: Create a function that finds the factorial of a given number using a `for` loop.
Question 22: Write a program that reverses a given array without using the `reverse()` method.

9. Logic and Problem-Solving

Question 23: Write a function that takes an array of numbers and returns the second largest
number.
Question 24: Create a function that takes an array and removes all duplicates, returning a new
array with only unique values.
Question 25: Write a program that takes a number as input and prints a triangle pattern using
nested `for` loops. For example, if the input is `5`:

markdown

*
**
***
****
*****

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/12
These questions will help you to practice coding and improve your problem-solving skills in JavaScript.
Try to solve each question, and if you get stuck, focus on breaking down the problem into smaller
parts. Happy coding!

now answer all of them

Let's work through all the questions with solutions and explanations:

1. Variables and Data Types

Question 1: Create a variable using `let` to store your name and another using `const` to store
your birth year. Change the value of the name variable but try changing the value of the birth
year. What happens?

javascript

let name = "John";


const birthYear = 1990;

// Change the value of the name variable


name = "Jane"; // Works fine

// Try changing the value of the birth year


birthYear = 1995; // Error: Assignment to constant variable

Explanation: `let` allows reassignment, so `name` can be changed to `"Jane"`. `const`


creates a constant, so trying to change `birthYear` results in an error.

Question 2: Write a program that stores three different data types (a string, a number, and a
boolean) in variables and prints their types using `typeof`.

javascript

let myString = "Hello";


let myNumber = 42;
let myBoolean = true;

console.log(typeof myString); // "string"


console.log(typeof myNumber); // "number"
console.log(typeof myBoolean); // "boolean"

2. Operators

Question 3: Write a program that takes two numbers as input and prints their sum, difference,
product, and quotient.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/12
javascript

let a = 8;
let b = 4;

console.log("Sum:", a + b); // 12
console.log("Difference:", a - b); // 4
console.log("Product:", a * b); // 32
console.log("Quotient:", a / b); // 2

Question 4: Compare two variables using both `==` and `===`. Explain the difference between
the two comparisons through code examples.

javascript

let x = 5;
let y = "5";

console.log(x == y); // true (checks value only)


console.log(x === y); // false (checks value and type)

Explanation: `==` compares only the values, so `5` and `"5"` are considered equal. `===`
compares both values and types, so `5` (number) is not equal to `"5"` (string).

3. Conditionals

Question 5: Write a program that checks if a number is positive, negative, or zero using
`if...else`.

javascript

let num = -10;

if (num > 0) {
console.log("Positive");
} else if (num < 0) {
console.log("Negative");
} else {
console.log("Zero");
}

Question 6: Create a `switch` statement that takes a day of the week as input (e.g., 1 for
Monday) and prints out the corresponding day name.

javascript

let day = 2;

switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/12
case 3:
console.log("Wednesday");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday");
break;
case 6:
console.log("Saturday");
break;
case 7:
console.log("Sunday");
break;
default:
console.log("Invalid day");
}

Question 7: Write a program that takes a user's age as input and outputs whether they are a
minor, an adult, or a senior citizen using `if`, `else if`, and `else`.

javascript

let age = 20;

if (age < 18) {


console.log("Minor");
} else if (age >= 18 && age < 60) {
console.log("Adult");
} else {
console.log("Senior Citizen");
}

4. Loops

Question 8: Write a `for` loop that prints numbers from 1 to 10.

javascript

for (let i = 1; i <= 10; i++) {


console.log(i);
}

Question 9: Write a `while` loop that prints the even numbers between 1 and 20.

javascript

let i = 2;
while (i <= 20) {
console.log(i);
i += 2;
}

Question 10: Create a `do...while` loop that asks the user for a password until they enter the
correct one.
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/12
javascript

let password;
const correctPassword = "1234";
do {
password = prompt("Enter the password:");
} while (password !== correctPassword);

console.log("Access granted.");

5. Advanced Loop

Question 11: Write a `for...of` loop that iterates over an array of colors and prints each color.

javascript

const colors = ["red", "green", "blue"];


for (let color of colors) {
console.log(color);
}

Question 12: Write a `for...in` loop that iterates over the properties of an object and prints
both the property names and their values.

javascript

const person = { name: "Alice", age: 25, city: "New York" };


for (let key in person) {
console.log(key + ": " + person[key]);
}

Question 13: Given an array of numbers, use a `for...of` loop to find the sum of all the
elements.

javascript

const numbers = [1, 2, 3, 4, 5];


let sum = 0;
for (let number of numbers) {
sum += number;
}
console.log("Sum:", sum); // Sum: 15

6. Arrays and Objects

Question 14: Write a program that stores five numbers in an array. Find and print the maximum
number using a loop.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/12
javascript

const nums = [5, 10, 15, 3, 8];


let max = nums[0];

for (let num of nums) {


if (num > max) {
max = num;
}
}

console.log("Max number:", max); // Max number: 15

Question 15: Create an object representing a car (with properties like `make`, `model`, `year`).
Write a function that takes an object as an argument and prints out all its properties.

javascript

const car = {
make: "Toyota",
model: "Corolla",
year: 2020
};

function printCarDetails(car) {
for (let key in car) {
console.log(key + ": " + car[key]);
}
}

printCarDetails(car);

Question 16: Write a function that takes an array of numbers as input and returns a new array
with each number squared.

javascript

function squareArray(arr) {
const squared = [];
for (let num of arr) {
squared.push(num * num);
}
return squared;
}

console.log(squareArray([1, 2, 3])); // [1, 4, 9]

7. Functions

Question 17: Write a function called `isEven` that takes a number as input and returns `true` if
it is even and `false` if it is odd.

javascript

function isEven(num) {
return num % 2 === 0;

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/12
}

console.log(isEven(4)); // true
console.log(isEven(5)); // false

Question 18: Write a function `calculateArea` that takes the radius of a circle as a parameter
and returns the area of the circle.

javascript

function calculateArea(radius) {
return Math.PI * radius * radius;
}

console.log(calculateArea(5)); // Approximately 78.54

Question 19: Create a function `greet` that takes a name as a parameter and prints a
personalized greeting.

javascript

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("Alice"); // Hello, Alice!

8. Practice Problems

Question 20: Write a program that checks if a given word is a palindrome (reads the same
forwards and backwards).

javascript

function isPalindrome(word) {
const reversed = word.split("").reverse().join("");
return word === reversed;
}

console.log(isPalindrome("racecar")); // true
console.log(isPalindrome("hello")); // false

Question 21: Create a function that finds the factorial of a given number using a `for` loop.

javascript

function factorial(n) {
let result = 1;
for (let i = 1; i <= n; i++) {
result *= i;
}
return result;
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 11/12
console.log(f

ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 12/12

You might also like