Js file doesn't work

I start learn JavaScript and I have a problem with my first project.
I have 3 file in a folder named ‘jd’, the files are: test.html, test1.js, and test2.js.


<!DOCTYPE html>
        Text din document JS. 
    <script async src='test1.js'></script>
    <script async src='test2.js'></script>

test1.js code

let xn = 1;
if(xn ==1){
 let xn =2;

test2.js code:

var xn = 1;
if(xn ==1){
 var xn =2;

When I run the code, the output is: "Text din document JS. "

IDE: Visual Studio Code

If I move the form test1.js directly in test.html code, it works.Preformatted text

Welcome to this forum.

The async attribute is telling the HTML parser not to wait for the JavaScript file to be be loaded (from local disk of from server). Each script will executed as soon as it becomes available: not necessarily test2.js after test1.js. Because these scripts are loaded asynchronously calls to document.write() are then ignored.

If you delete async the calls to document.write will not be ignored. However you will then get a failure because you are declaring variable xn more than once. Nevertheless you will get this in your browser:
Text din document JS. 2
If you declare variable xn only once you will see:
Text din document JS. 2 2

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.