Course JavaScript Fundamentals

Objects

Functions & Data Preview

Objects

const student = {\n  name: "Ama",\n  level: "200",\n  courses: ["PHP", "JS"],\n  graduated: false,\n};\n\nstudent.name;        // "Ama"\nstudent["level"];    // "200"\nstudent.courses[0];  // "PHP"

Methods

const calc = {\n  add(a, b) { return a + b; },\n  sub(a, b) { return a - b; },\n};\ncalc.add(4, 3);  // 7

Updating & adding

student.graduated = true;\nstudent.city = "Kumasi";   // adds a new key

Looping keys

for (const key in student) {\n  console.log(key, student[key]);\n}\n\nObject.keys(student);   // ["name", "level", ...]\nObject.values(student); // ["Ama", "200", ...]

Destructuring

const { name, level } = student;\nconsole.log(name);  // "Ama"

Objects vs arrays

Objects group named data about one thing; arrays store an ordered list. Use both together constantly.

Tip: Spacing your code with empty lines between logical sections makes large objects and functions far easier to read.

This is a free preview lesson

Enroll in the JavaScript Fundamentals to unlock every lesson, lesson PDF guides, quizzes and your certificate - and start creating your own pieces today.


Discussion

Free preview lesson