Skip to main content

Javascript, explained.

Published 2022/02/10 14:00

Javascript is the part of a website that is responsible for responding to user actions. This can really make a website come to life! How does it do that?

Any website must contain some content. The content of a website is handled by a language called HTML.

HTML, explained

For example, if you want to add a heading and a paragraph, underneath it

the HTML would look like this:

<h1>Hello</h1>
<p id="my-name">My name is Savvas</p>

What a website looks like, is handled by another language called CSS.

CSS, explained

For example, if you want the paragraph (<p>) called my-name from above to have a red text,

you would use CSS like this:

#my-name{
  color: red;
}

Now suppose we want to make it so that whenever the user clicks a button, a box appears with a message.

Can we do this in HTML?

No, because HTML is responsible for the content of a page.

Can we do this in CSS?

No, because CSS is responsible with how a website looks.

So what can we use? This is where Javascript comes in!

Enter Javascript

Javascript is the third and last of the languages for building websites. It's what adds interaction to a website.

So let's do this!

First of all, let's create a button the user can click on.

Let's update our HTML first to add our button:

<h1>Hello</h1>
<p id="my-name">My name is Savvas</p>

+ <button>Show me a message!</button>

To use Javascript, let's create a file called popup.js which will contain our code for the popup.

Next, we're going to add a line in our <head> of our HTML, to read the file:

<head>
    <script src="popup.js"></script>
</head>

In the popup.js file, we're going to create a function which will show a popup when called. Let's call it showPopup:

function showPopup(){

}

Now, add something in our function to show the popup itself (or an alert in Javascript):

function showPopup(){
    alert("Hello!")
}

Now that our function is ready, let's tell our button that there's a function you want to execute when it's clicked.

All we need to do is update our <button> element with an onclick which contains our function name:

<button onclick="showPopup()">Show me a message!</button>

And that's it!

Now when the user clicks the button, a popup (alert) will appear saying "Hello!".

Congratulations! You've added your first interaction to your website! You made your website come to life!

Now there are lots of things you can do, like changing colours, adding and removing elements, even code entire games that are playable in your browser using Javascript!

For a proper introduction to Javascript, I suggest this 3.5 hour free tutorial from freeCodeCamp:

Thank you for reading!

Dev, Explained (43 part series)

  1. Javascript Scopes, explained.
  2. Javascript Promises, explained.
  3. Accessibility, explained.
  4. React, explained
  5. Should I use forEach() or map()?
  6. Should I use Flexbox or CSS Grid?
  7. Docker, explained.
  8. Unit testing, explained
  9. Git, explained.
  10. Typescript, explained.
  11. async/await, explained.
  12. The DOM, explained.
  13. Regular expressions, explained
  14. GraphQL, explained.
  15. Vue, explained.
  16. Svelte, explained.
  17. API, explained.
  18. Javascript Hoisting, explained.
  19. Immediately Invoked Function Expressions (IIFE), explained.
  20. ARIA roles, explained.
  21. Test-driven Development, explained.
  22. ARIA live regions, explained.
  23. aria-label in accessibility, explained.
  24. Type coercion in Javascript, explained.
  25. Variables, explained.
  26. if statements, explained.
  27. Arrays, explained.
  28. Currying in Javascript, explained.
  29. Memoization, explained.
  30. For loops, explained.
  31. Javascript Prototypes, explained.
  32. React Hooks, explained.
  33. Graph databases, explained.
  34. MongoDB, explained.
  35. Serverless, explained.
  36. Javascript Callback functions, explained.
  37. HTML, explained.
  38. CSS, explained.
  39. Responsive design, explained.
  40. Javascript, explained.
  41. The CSS Box Model, explained.
  42. CSS Flexbox, explained.
  43. CSS Grid, explained.
2022 Savvas Stephanides
Buy me a coffee
Some icons from Freepik