Give me an entire basic tutorial on JS covering: printing, data types, variables, for…
- User
Give me an entire basic tutorial on JS covering: printing, data types, variables, for loops, while loops, conditional statements and common JS functions (e.g. map, reduce) along with some practice questions
Assistant
JavaScript Basics Tutorial
1. Printing in JavaScript
JavaScript provides several ways to print output:
console.log()→ Prints to the browser console (most common)alert()→ Shows a pop-up messagedocument.write()→ Writes directly to the HTML document (rarely used)
Example:
console.log("Hello, World!"); // Output: Hello, World! alert("Welcome to JS!"); // Shows a pop-up document.write("JS is fun!"); // Writes to the webpage
2. Data Types in JavaScript
JavaScript has dynamic typing (variables can change types). The main data types are:
Type Example Description Number42,3.14Integers and decimals String"Hello",'JS'Text in quotes Booleantrue,falseLogical true/false NullnullExplicitly empty value UndefinedundefinedVariable not assigned a value Object{ name: "Alice", age: 25 }Key-value pairs (like dictionaries) Array[1, 2, 3]Ordered lists Example:
let age = 25; // Number let name = "Alice"; // String let isStudent = true; // Boolean let empty = null; // Null let unknown; // Undefined let person = { // Object name: "Bob", age: 30 }; let numbers = [1, 2, 3]; // Array
3. Variables in JavaScript
Variables store data. There are three ways to declare them:
var→ Old way (avoid in modern JS)let→ Block-scoped, can be reassignedconst→ Block-scoped, cannot be reassigned
Example:
var oldWay = "Avoid me"; // Function-scoped (outdated) let changeable = 10; // Can be reassigned changeable = 20; // Valid const fixed = 100; // Cannot be reassigned // fixed = 200; // Error!
4. For Loops
Used to repeat code a specific number of times.
Syntax:
for (initialization; condition; increment) { // Code to repeat }Example:
for (let i = 0; i < 5; i++) { console.log(i); // Prints 0, 1, 2, 3, 4 }
5. While Loops
Repeats code while a condition is true.
Syntax:
while (condition) { // Code to repeat }Example:
let count = 0; while (count < 3) { console.log(count); // Prints 0, 1, 2 count++; }
6. Conditional Statements
Used to make decisions in code.
if,else if,elselet age = 18; if (age < 13) { console.log("Child"); } else if (age < 20) { console.log("Teen"); // This runs } else { console.log("Adult"); }switchStatementlet day = "Monday"; switch (day) { case "Monday": console.log("Start of the week"); break; case "Friday": console.log("Weekend is near"); break; default: console.log("Midweek"); }
7. Common JavaScript Functions
Array Methods
Method Example Description map()arr.map(x => x * 2)Transforms each element filter()arr.filter(x => x > 2)Keeps elements that pass a test reduce()arr.reduce((a, b) => a + b)Combines elements into one value forEach()arr.forEach(x => console.log(x))Loops through each element Example:
let nums = [1, 2, 3]; let doubled = nums.map(x => x * 2); // [2, 4, 6] let evens = nums.filter(x => x % 2 === 0); // [2] let sum = nums.reduce((a, b) => a + b); // 6 nums.forEach(num => console.log(num)); // Prints 1, 2, 3
8. Practice Questions
Easy:
- Print numbers from 1 to 10 using a
forloop. - Check if a number is even or odd using
if-else. - Use
map()to convert["apple", "banana"]to uppercase.
Medium:
- Sum all numbers in an array using
reduce(). - Use a
whileloop to print numbers from 5 to 1. - Filter an array to only include numbers greater than 5.
Hard:
- Find the largest number in an array using a loop.
- Write a function that checks if a word is a palindrome.
- Use
reduce()to count occurrences of each word in an array.
Solutions
1. Print 1 to 10
for (let i = 1; i <= 10; i++) { console.log(i); }2. Even/Odd Check
let num = 7; if (num % 2 === 0) { console.log("Even"); } else { console.log("Odd"); }3. Uppercase with
map()let fruits = ["apple", "banana"]; let upperFruits = fruits.map(fruit => fruit.toUpperCase()); console.log(upperFruits); // ["APPLE", "BANANA"]4. Sum with
reduce()let nums = [1, 2, 3]; let sum = nums.reduce((a, b) => a + b, 0); console.log(sum); // 65.
whileLoop (5 to 1)let i = 5; while (i >= 1) { console.log(i); i--; }6. Filter >5
let nums = [3, 8, 1, 6]; let filtered = nums.filter(x => x > 5); console.log(filtered); // [8, 6]7. Largest Number in Array
let nums = [4, 2, 9, 5]; let max = nums[0]; for (let num of nums) { if (num > max) max = num; } console.log(max); // 98. Palindrome Check
function isPalindrome(word) { return word === word.split("").reverse().join(""); } console.log(isPalindrome("madam")); // true9. Word Count with
reduce()let words = ["apple", "banana", "apple"]; let count = words.reduce((acc, word) => { acc[word] = (acc[word] || 0) + 1; return acc; }, {}); console.log(count); // { apple: 2, banana: 1 }
Conclusion
This tutorial covers the basics of JavaScript, including printing, data types, loops, conditionals, and common functions. Practice the questions to reinforce your learning! 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.