HCDD 411
1 / 18

JavaScript Syntax - Part 2

Md Touhidul Islam

What You'll Learn

🎯 Objects

Creating and manipulating data structures

📦 Arrays

forEach, map, filter, every, some

⚡ ES6 Features

arrow functions, classes

JavaScript Objects

Key-Value Pairs

PLAYGROUND: Create Objects
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Objects - Manipulate Properties

Access Properties

Add/Update Properties

Delete Properties

EXPERIMENT: Object Operations
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

JavaScript Arrays

Array Basics

EXPLORE: Array Basics
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Methods - Part 1

Common Array Methods

TRY IT: Array Methods
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Method: forEach()

forEach() - Iterate through each element and execute a function
  • Does NOT return anything
  • Parameters: element value, element index, entire array (all optional)
EXPERIMENT: forEach()
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Method: map()

map() - Create a new array by transforming each element
  • RETURNS a new array
  • Must use return keyword
  • Original array is NOT modified
TRANSFORM: map()
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Method: filter()

filter() - Create a new array with elements that pass a condition
  • RETURNS a new array with filtered elements
  • Must use return with a conditional statement
  • Only elements that return true are included
FILTER: Find Elements
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Method: every()

every() - Check if ALL elements satisfy a condition
  • RETURNS true only if ALL elements pass
  • Returns false if ANY element fails
  • Must use return with a conditional statement
CHECK: Test All Elements
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Array Method: some()

some() - Check if AT LEAST ONE element satisfies a condition
  • RETURNS true if ANY element passes
  • Returns false only if ALL elements fail
  • Must use return with a conditional statement
CHECK: Test Any Element
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

ES6 - Classes

ES6 introduced class syntax for object-oriented programming
  • Create blueprints for objects
  • Use extends for inheritance
  • Much cleaner than prototype-based approach
BUILD: Create Classes
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

ES6 - Arrow Functions

Shorter Function Syntax

TRANSFORM: Arrow Functions
📄 script.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Practice Problem 1: Arrow Function Converter

🎯 Your Challenge:
Convert traditional functions to arrow functions and use them with array methods.

Instructions:

💡 Example Output:
Tripled: [15, 30, 45, 60, 75, 90]
Filtered: [20, 25, 30]
20
25
30
YOUR CODE: Arrow Functions Practice
📄 practice1.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Practice Problem 2: Object & Array Manipulation

🎯 Your Challenge:
Create an array of student objects and use array methods to manipulate the data.

Instructions:

💡 Example Output:
High achievers: [{name: "Alice", ...}]
Names: ["Alice", "Bob", "Carol"]
All passing: true
YOUR CODE: Work with Student Data
📄 practice2.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output:

Practice Problem 3: ES6 Class Challenge

🎯 Your Challenge:
Create a class hierarchy for a simple game with characters.

Instructions:

💡 Hint: Use super() in the child class constructor to call the parent constructor.
YOUR CODE: Build Character Classes
📄 practice3.js
Ln 1, Col 1
JavaScript
Tab Indent Ctrl+Enter Run Ctrl+/ Comment
💻 Console Output: