101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Objects and Arrays

    Understanding Objects in JavaScript

    high-level programming language

    High-level programming language.

    In JavaScript, an object is a standalone entity with properties and types. It is like a container that holds related data and functionality. Objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So, let's dive in and learn about JavaScript objects.

    Definition of Objects in JavaScript

    In JavaScript, an object is a non-primitive data type that allows you to store multiple values as a complex data structure. The values are written as name:value pairs, also known as properties. The name can be a string or a symbol, and the value can be any data type, including other objects, which enables building complex data structures.

    Creating Objects Using Object Literals

    The simplest way to create an object in JavaScript is to use an object literal. An object literal is a comma-separated list of name-value pairs inside of curly braces. Here's an example:

    let person = { name: 'John', age: 30 };

    Adding, Modifying, and Deleting Properties

    You can add new properties to an existing object, or change the value of existing properties, using dot notation or bracket notation. For example:

    // Adding a new property person.job = 'Engineer'; // Modifying an existing property person.age = 31;

    To delete a property from an object, you can use the delete operator:

    delete person.job;

    Accessing Object Properties

    You can access the properties of an object using dot notation or bracket notation. Dot notation is more concise and easier to read, but bracket notation is more versatile because it can evaluate an expression to get a property name.

    // Dot notation console.log(person.name); // Outputs: John // Bracket notation console.log(person['age']); // Outputs: 30

    Understanding Methods and Adding Methods to Objects

    In an object, properties can also be functions. When a function is a property of an object, we call it a method. Methods are used to represent actions that an object can perform. Here's how to add a method to our person object:

    person.sayHello = function() { console.log('Hello, my name is ' + this.name); }; person.sayHello(); // Outputs: Hello, my name is John

    The this Keyword in Objects

    In the context of an object, this refers to the "owner" of the function we're executing, or rather, to the object that a function is a method of. When we used this in our sayHello method, it referred to the person object, because sayHello is a method of person.

    In conclusion, understanding objects is a fundamental part of mastering JavaScript, as they provide a way to group related data and functionality together. By understanding how to create, manipulate, and access objects, you can write more organized and efficient code.

    Test me
    Practical exercise
    Further reading

    Hi, any questions for me?

    Sign in to chat
    Next up: Understanding arrays