[Go to site: main page, start]

0% found this document useful (0 votes)
10 views2 pages

JavaScript Practice Workbook: Weeks 1-2

The document outlines a JavaScript practice workbook for Weeks 1 and 2, covering fundamental concepts such as variables, functions, loops, and arrays in Week 1. Week 2 introduces intermediate topics including objects, destructuring, spread/rest operators, DOM manipulation, and event listeners. Each section includes specific practice exercises to reinforce learning and application of the concepts.

Uploaded by

ajay.tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

JavaScript Practice Workbook: Weeks 1-2

The document outlines a JavaScript practice workbook for Weeks 1 and 2, covering fundamental concepts such as variables, functions, loops, and arrays in Week 1. Week 2 introduces intermediate topics including objects, destructuring, spread/rest operators, DOM manipulation, and event listeners. Each section includes specific practice exercises to reinforce learning and application of the concepts.

Uploaded by

ajay.tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript Practice Workbook - Week 1 & Week 2

Week 1: JavaScript Basics - Practice & Exercises

- Variables & Data Types (var, let, const)

- Practice:

1. Declare variables for name, age, isStudent.

2. Try re-assigning values to let vs const.

- Functions + Arrow Functions

- Practice:

1. Write a function that returns square of a number.

2. Convert it to an arrow function.

3. Write a function to return the larger of two numbers.

- Loops (for, while)

- Practice:

1. Print numbers from 1 to 10 using for and while loop.

2. Loop through an array of colors.

- if/else, switch

- Practice:

1. Write a function that checks if a number is positive, negative or zero.

2. Use switch to print day of the week for a number 1-7.

- Arrays (map, filter, reduce)

- Practice:

1. Use map to square each number in an array.

2. Use filter to get even numbers.

3. Use reduce to sum the values.

Week 2: Intermediate JavaScript - Practice & Exercises


JavaScript Practice Workbook - Week 1 & Week 2

- Objects

- Practice:

1. Create a student object with name, age, marks.

2. Write a method to greet using object.

- Destructuring

- Practice:

1. Destructure name and age from student object.

2. Destructure values from an array of fruits.

- Spread/Rest operators

- Practice:

1. Combine two arrays using spread.

2. Create a function that uses rest to accept any number of arguments.

- DOM Manipulation

- Practice (HTML + JS):

1. Change the text of an <h1> element using JS.

2. Toggle the background color of the page on button click.

- Event Listeners + Bubbling

- Practice:

1. Add a click event to a button that shows an alert.

2. Add bubbling example with nested divs and stop propagation.

Common questions

Powered by AI

Arrow functions allow for a shorter syntax compared to traditional function expressions and do not bind their own 'this', making them ideal for methods that do not require a separate context. They are particularly useful in callbacks and methods like 'map' or 'filter'. An arrow function expression is syntactically shorter: without the 'function' keyword and curly braces (for single expressions), and with a simple '=>'. This makes them less verbose and more concise .

The 'map' method is beneficial for creating a new array by applying a function to each element in the original array, which is useful for transformations. 'Filter' creates a new array that includes only elements that meet certain conditions, making it ideal for queries. 'Reduce' accumulates all elements in an array to a single value, providing powerful tools for summarizing data. These methods offer a convenient, readable, and functional-programming approach to array manipulation .

A function to determine if a number is positive, negative, or zero can use an if-else ladder: if the number is greater than 0, return 'positive'; if less than 0, return 'negative'; otherwise, return 'zero'. This logic utilizes JavaScript's ability to compare numerical values directly within conditional statements to categorize the number .

In JavaScript, variables declared with 'let' can be reassigned new values within their scope, while 'const' variables cannot be reassigned after their initial assignment. When attempting to reassign a 'const' variable, JavaScript will throw an error. This is because 'const' is used for values that should remain constant throughout the program execution .

Functions and arrow functions facilitate code modularity and reuse by encapsulating frequently used logic in reusable units. Arrow functions enhance this by offering succinct syntax for simple operations and do not create their 'this' context, making them suitable for callback functions in asynchronous operations. Both types of functions can be stored, passed as arguments, or returned by other functions, promoting DRY (Don't Repeat Yourself) principles and easier maintenance .

Event bubbling occurs when an event triggers on the innermost child element and propagates up to its ancestors. To control this behavior, JavaScript provides methods like 'stopPropagation()', which stops the further propagation of the event in the bubbling phase. By properly managing event listeners and utilizing event methods, developers can prevent certain events from affecting parent or ancestor elements unintentionally .

The spread operator expands an array's or object's elements into individual parts, which is useful for combining or cloning arrays and objects. It is placed before an iterable in a new context. In contrast, rest parameters collect multiple elements into a singular array within function definitions, allowing functions to accept any number of arguments. The spread operator is employed in expansion situations, while rest is used for aggregation .

Destructuring allows unpacking values from arrays or properties from objects into distinct variables. It provides a cleaner, more readable syntax for extracting multiple properties in a single statement. For example, destructuring can be used to assign object properties to variables in one line or to initialize several variables from array elements simultaneously. This technique simplifying code by reducing the number of lines and clarifying the data flow .

JavaScript uses 'for' and 'while' loops to iterate over array elements systematically. A 'for' loop initializes an index variable to track the current element, increments it with each iteration, and loops until all elements are processed. Alternatively, 'Array.forEach()' can be employed, providing a more functional style. These loops enable the execution of code for each array element, such as calculations or updates .

JavaScript can modify the DOM by selecting an HTML element using methods like 'getElementById' or 'querySelector', then changing its text using 'innerText' or 'textContent'. To toggle styles, such as background color, JavaScript can change CSS properties directly via the 'style' property or toggle classes that define specific styles using 'classList.toggle'. These techniques allow dynamic, interactive web page updates without requiring a page reload .

You might also like