append VS appendChild
Abdulqudus Abubakre

Abdulqudus Abubakre @ibn_abubakre

About: Front end developer, JavaScript enthusiast, Community Builder

Location:
Abuja, Nigeria
Joined:
Jan 3, 2020

append VS appendChild

Publish Date: Apr 17 '20
208 28

This is the first post in the this vs that series. A series aimed at comparing two often confusing terms, methods, objects, definition or anything frontend related.

append and appendChild are two popular methods used to add elements into the Document Object Model(DOM). They are often used interchangeably without much troubles, but if they are the same, then why not scrape one....Well they are only similar, but different. Here's how:

.append()

This method is used to add an element in form of a Node object or a DOMString (basically means text). Here's how that would work.

// Inserting a Node object
const parent = document.createElement('div');
const child = document.createElement('p');
parent.append(child);
// This appends the child element to the div element
// The div would then look like this <div><p></p></div>
Enter fullscreen mode Exit fullscreen mode
// Inserting a DOMString
const parent = document.createElement('div');
parent.append('Appending Text');
// The div would then look like this <div>Appending Text</div>
Enter fullscreen mode Exit fullscreen mode

.appendChild()

Similar to the .append method, this method is used to elements in the DOM, but in this case, only accepts a Node object.

// Inserting a Node object
const parent = document.createElement('div');
const child = document.createElement('p');
parent.appendChild(child);
// This appends the child element to the div element
// The div would then look like this <div><p></p></div>
Enter fullscreen mode Exit fullscreen mode
// Inserting a DOMString
const parent = document.createElement('div');
parent.appendChild('Appending Text');
// Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'
Enter fullscreen mode Exit fullscreen mode

Differences

  1. .append accepts Node objects and DOMStrings while .appendChild accepts only Node objects

    const parent = document.createElement('div');
    const child = document.createElement('p');
    // Appending Node Objects
    parent.append(child) // Works fine
    parent.appendChild(child) // Works fine
    // Appending DOMStrings
    parent.append('Hello world') // Works fine
    parent.appendChild('Hello world') // Throws error
    
  2. .append does not have a return value while .appendChild returns the appended Node object

    const parent = document.createElement('div');
    const child = document.createElement('p');
    const appendValue = parent.append(child);
    console.log(appendValue) // undefined
    const appendChildValue = parent.appendChild(child);
    console.log(appendChildValue) // <p><p>
    
  3. .append allows you to add multiple items while appendChild allows only a single item

    const parent = document.createElement('div');
    const child = document.createElement('p');
    const childTwo = document.createElement('p');
    parent.append(child, childTwo, 'Hello world'); // Works fine
    parent.appendChild(child, childTwo, 'Hello world');
    // Works fine, but adds the first element and ignores the rest
    

Conclusion

In cases where you can use .appendChild, you can use .append but not vice versa.

That's all for now, if there are any terms that you need me to shed more light on, you can add them in the comments section or you can reach me on twitter

Comments 28 total

  • Ben Halpern
    Ben HalpernApr 17, 2020

    Very clear guide

  • Tulsi Prasad
    Tulsi PrasadApr 18, 2020

    Very consice and clear explanation, thanks!

  • Aliyu Abubakar
    Aliyu AbubakarApr 18, 2020

    This is straightforward

  • Anthonia Okafor
    Anthonia OkaforApr 18, 2020

    Well explained

  • Pacharapol Withayasakpunt
    Pacharapol WithayasakpuntApr 18, 2020

    It would be nice if you also compare the speed / efficiency.

  • dAMi
    dAMiApr 18, 2020

    This is helpful, thanks boss!

  • Md. Monirul Islam
    Md. Monirul IslamMay 16, 2020

    Thanks man!

  • Ronak Jethwa
    Ronak JethwaMay 24, 2020

    Nice one! Few more suggestions for the continuation of the series!

    1. Call vs Apply
    2. Prototype vs __proto__
    3. Map vs Set
    4. .forEach vs .map on Arrays
    5. for...of vs for...in
    
    Enter fullscreen mode Exit fullscreen mode
  • Navin
    NavinOct 9, 2020

    Very well explained !!

  • mmestiyak
    mmestiyakOct 18, 2020

    Thanks man!

  • Abdelrahman Hassan
    Abdelrahman HassanJan 23, 2021

    Excellent explanation

  • Frupreneur
    FrupreneurApr 3, 2022

    "In cases where you can use .appendChild, you can use .append but not vice versa."

    well if you do need that return value, appendChild does the job while .append doesnt

    • Ryan Zayne
      Ryan ZayneJul 21, 2024

      Would anyone ever need that return value tho?

      • Mao
        MaoJan 27, 2025

        There's a lot of instances where you need the return value, if you need to reference it for a webapp / keep it somewhere / change its properties

  • MLM
    MLMMay 25, 2022

    I UNDERSTAND, FINALLY! New to JavaScript and DOM Manipulation, this was perfect, clear, concise! Thank you!

  • jamaicayancy
    jamaicayancyJan 21, 2023

    This was perfect. Thanks!

  • mohammadparsa-javidi
    mohammadparsa-javidiJan 22, 2023

    Perfect👌

  • Ali-alterawi
    Ali-alterawiApr 20, 2023

    thank you

  • Kelsie Paige
    Kelsie PaigeJul 6, 2023

    Here’s the light bulb moment I’ve been looking for. You nailed it in such a clean and concise way that I could understand as I read and didn’t have to reread x10 just to sort of get the concept. That’s gold!

  • Berton Bentley
    Berton BentleyMay 31, 2024

    still choose append

  • Rashid Enahora
    Rashid EnahoraOct 20, 2024

    Thank you sir!!! That was very clear and concise!!!

Add comment