Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run code I have written in JavaScript?

Tags:

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.

like image 472
user2607534 Avatar asked Aug 07 '15 21:08

user2607534


People also ask

How do I run JavaScript code?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

Where do I write and run JavaScript?

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.

How do I run a JavaScript file in Windows 10?

To enter JavaScript statements and expressions interactively in the Console: Right-click in a webpage and then select Inspect. DevTools opens. Or, press Ctrl + Shift + J (Windows, Linux) or Command + Option + J (macOS), to directly open the DevTools console.


3 Answers

  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.

like image 56
DhruvJoshi Avatar answered Oct 06 '22 17:10

DhruvJoshi


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:

  • Your browser console, I believe pressing F12 will open it on all popular browsers
  • JS Console
  • Node.js's REPL (installation instructions)
like image 3
Nabil Kadimi Avatar answered Oct 06 '22 16:10

Nabil Kadimi


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
like image 3
Randula Koralage Avatar answered Oct 06 '22 16:10

Randula Koralage