Basic Syntax

// define variable
const hello = "Hello World!";

//console.log to print output to console (in dev tools)
console.log(hello);

// defining a function
function printMessage(message) {
    console.log(message);
}

// calling a function
printMessage("Hello from a function!");
Hello World!
Hello from a function!

Creating objects

// function to create data for person (from https://nighthawkcoders.github.io/APCSA//techtalk/javascript)
function Person(name, role) {
    this.name = name;
    this.role = role;
}

const womenInStem = [
    new Person("Mads", "deployment"),
    new Person("Meens", "backend"),
    new Person("Prans", "scrum master"),
    new Person("Shish", "frontend")
]

console.log(womenInStem)
[ Person { name: 'Mads', role: 'deployment' },
  Person { name: 'Meens', role: 'backend' },
  Person { name: 'Prans', role: 'scrum master' },
  Person { name: 'Shish', role: 'frontend' } ]

Project Prototype

Create HTML page for basic commerce site.

// create object for listing
function Listing(name, price, contactInfo) {
    this.name = name;
    this.price = price;
    this.contactInfo = contactInfo;
}

const data = [
    new Listing("TINspire Calculator", "70.00", "test@test.com"),
    new Listing("New PE Lock", "5.00", "1-800-YOUR-NUMBER"),
    new Listing("AP Bio Prep Book", "15,00", "test@test.com")
]

// return table row for each listing that can be added to html
for (const row of data) { 
    const name = row.name;
    const price = row.price;
    const contactInfo = row.contactInfo;
    console.log(`
    <tr>
        <td>${name}</td>
        <td>${price}</td>
        <td>${contactInfo}</td>
    </tr>
    `)
 }
    <tr>
        <td>TINspire Calculator</td>
        <td>70.00</td>
        <td>test@test.com</td>
    </tr>
    

    <tr>
        <td>New PE Lock</td>
        <td>5.00</td>
        <td>1-800-YOUR-NUMBER</td>
    </tr>
    

    <tr>
        <td>AP Bio Prep Book</td>
        <td>15,00</td>
        <td>test@test.com</td>
    </tr>