user2607534
user2607534

Reputation: 221

How do I run code I have written in JavaScript?

I am a beginner, all I have done is practiced writing code in Codecademy. After extensive searches of google for how to run a .js file, I turned up nothing. I assume I am asking the wrong question, and I am sure it is simple, but I couldn't find anything.

Upvotes: 15

Views: 34308

Answers (6)

Randula Koralage
Randula Koralage

Reputation: 338

First install and setup Nodejs in your machine. Then write your javascript and save it in a folder. Open the command prompt inside the folder and write the command to execute.

node a.js

Upvotes: 4

Nabil Kadimi
Nabil Kadimi

Reputation: 10384

This is an addition to previous answers.

If you want to practice simple JavaScript instructions and code snippets like you did in Codecademy you can use:

Upvotes: 3

DhruvJoshi
DhruvJoshi

Reputation: 17126

  1. open an editor. Simplest one is notepad
  2. Write basic HTML there like below

    <html>
        <head>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  3. Add a script tag and write your js inside it like below

    <html>
        <head>
            <script> 
                alert("hello");
            </script>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  4. or you can write your js code in a file and save as .js file and link that in the above code

    <html>
        <head>
            <script src="myScript.js"></script>
        </head>
        <body>
            Hello World!
        </body>
    </html>
    
  5. Save this as yourfile.HTML and open in any browser

Here's a link to learn more: http://www.w3schools.com/js/js_whereto.asp.

Upvotes: 15

Pavel Petrovich
Pavel Petrovich

Reputation: 764

Run javascript in your browser simply use this methods:

1. use jsfiddle.net

2. use developer console your browser (how to open console in Chrome/Firefox/Safari you can read in Wiki)

3. write your own file with extention .html and put it:

<script>
    alert('Hello world!');
</script>

into the file, save file and open on browser.

Every method have own benefits when you discover JS. We developers use every day all of this methods.

Upvotes: 2

Nathu
Nathu

Reputation: 262

When running a .js file, you just need to add it in your web page. Example.

If you have this as the content of a .js file say hello.js

alert('Yow!');

to use it, you create an HTML file

<html>
    <body>
        <script src="hello.js"></script>
    </body>
</html>

Upvotes: 1

Mr.Web
Mr.Web

Reputation: 7144

You are to create a file with extension .html, so open up notepad or similar program and write the following:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript">
    //Your code
    alert("You made your fist javascript!");
    </script>

</body>
</html>

Your javascript actions and codes go inside the <script> tag.

Upvotes: 1

Related Questions