DEV Community

Cover image for JavaScript Rendered Hello World
Bipin Rajbhar
Bipin Rajbhar

Posted on β€’ Edited on

12 5

JavaScript Rendered Hello World

In this article, we are not going to use React at all. Instead, we are going to use JavaScript to create a div DOM element with the text content "Hello World".

Why we are doing this?
It's very important to have a basic understanding of how the DOM elements are created using JavaScript because it will help you to understand how React really works under the hood.

Exercise 1

<!DOCTYPE html>
<html>
  <head>
    <title>Excercise</title>
  </head>
  <body>
    <!-- create a 'div' element with an id 'root' -->

    <script>
      // create a 'div' element
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

      // set the textContent of div element to 'Hello World'
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

      // append the div element to root div using append method
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Solution Code (Exercise 1)

Exercise 2

<!DOCTYPE html>
<html>
  <head>
    <title>Excercise</title>
  </head>
  <body>
    <script>
      // create a root 'div' element
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

      // set id attribute to 'root' for root div element
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

      // append the root div element to body using append
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append

      // create a 'div' element
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

      // set the textContent of div element to 'Hello World'
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent

      // append the div element to root div using append method
      // πŸ“œ https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append
    </script>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Solution Code (Exercise 2)

I hope you learned something from this article and if you like this article then do not forget to give heart, unicorn, etc.

My name is Bipin Rajbhar and I am a software engineer at QuikieApps and you can follow or connect to me on Twitter and Linked In

Resources
The Beginner's Guide to React
Epic React

Top comments (2)

Collapse
 
yoursunny profile image
Junxiao Shi β€’

The xo linter says you are supposed to use querySelector than getElementById.

Collapse
 
tobto profile image
Yuri Polchenko β€’

the dream

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay