Where do i write javascript code?

Where do i write javascript code?

Where do i write javascript code?

In this new video 🎬 I explain and show the 3 ways of how to write and execute your JavaScript code.

👩🏻‍💻 Where to write and execute JavaScript

  1. Writing and executing JavaScript directly in the Browser Console is the simplest way to get started. It's practical when you want to try something out, but not an option for real programming. Find out why in the video.
  2. The second option writing JavaScript code in the index.html file (called inline script) is better, but has some disadvantages in structuring and keeping HTML/CSS apart from your logic.
  3. The third and last way to write JavaScript code, is writing the code in a separate JavaScript file and linking this file in the HTML file. This is the way to go, because it is clean and practical when your application gets bigger.

👩🏻‍💻 Simple Text Editor vs. Special Code Editor
I also explain and show you the advantages of writing your JavaScript code in a special code editor instead of writing it in a simple text editor. One example is the highlighting of keywords or syntax error detection, which helps a lot in programming! 💯

👩🏻‍💻 Finally, understanding how files in real projects are structured and referenced will help you a lot when getting started.

Really understanding the above topics, will help you a lot when starting coding. 🙂


✅ I will upload a complete JavaScript Tutorial for absolute beginners. So if you are completely new to programming this will give you a step by step guide to learn JavaScript. You can subscribe on Youtube to get notified 🙂 ✅

I'm happy to connect with you on

  • Twitter
  • YouTube
  • Instagram

Updated: 05/03/2022 by

Where do i write javascript code?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor.

Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor. A basic HTML file has a docType and some basic HTML tags, such as <html>, <head> and <body>. For example, a basic HTML5 file might look something like what is shown below.

<!DOCType HTML>
<html>
<head>
<title>Testing JavaScript</title>
</head>
<body>
[Content goes here]
</body>
</html>

When you see JavaScript code on the Web, you will sometimes see some JavaScript code between the <head></head> tags. Or, you may see it in the <body></body> tags (or even in both places). To separate JavaScript code from HTML code, you need to enclose it within a set of <script></script> tags. The opening <script> tag has one required attribute and one optional attribute. The required attribute is the type attribute, while the optional attribute is src (which lets you point to an external script file, covered later in this answer). The value of the type attribute is set to text/javascript, as shown below.

<script type="text/javascript">
Your JavaScript code goes here.
</script>

As you can see, your JavaScript code is placed between the opening and closing script tags. For example script, you could write a simple string of text directly on the web page, as shown below (placed between the <body> and </body> tags).

As you can see, your JavaScript code is placed between the opening and closing script tags. For example script, you could write a simple string of text directly on the web page, as shown below (placed between the <body> and </body> tags).

<script type="text/javascript">
document.write("Text written using JavaScript code!");
</script>

In the example above, the standard JavaScript function displays text between the quotation marks on the page. It would look like the example below.

Another option for including JavaScript on a page is creating the script in an external text file. Save that file with a .js extension, making it a JavaScript file. The .js file is then included in the page using the src attribute of the opening script tag. For example, to use the script above, place the JavaScript code (without script tags) into a new text file, as shown below.

document.write("Text written using JavaScript code!");

You would then save the file with a .js extension. For instance, you could save it as write.js. Once the file is saved, you can call it from the HTML code via the src attribute of the opening script tag, as shown below for write.js.

<script type="text/javascript" src="write.js"></script>

The procedure above has the same effect as writing the code between the script tags, but won't clutter the HTML code with JavaScript. Another advantage is that the same script can be included in multiple pages, and editing the script file updates the script in every page that uses the external script file. Editing the script file is helpful as it can be done in one place, rather than editing the code on each page containing the script.

Now knowing how to add JavaScript code to a web page, you can use any number of JavaScript example scripts or follow a JavaScript tutorial or book to learn more about JavaScript coding.