How to program Javascript using Eclipse

Eclipse can be used as an IDE for Javascript programming. This is useful if you like to have autocompletion and syntax highlighting when you are editing or reading Javascript code.

  • To work with projects that have HTML, CSS and Javascript, we need to install the Eclipse Web Tools Platform (WTP). The easiest way to get this is to install one of the all-in-one Eclipse packages that has WTP in it. For example, you can download the Eclipse Java EE package since it has WTP.

  • Open Eclipse and create a new Project. Choose Web > Static Web Project. This allows you to have HTML, CSS and Javascript files.

  • Right-click on the project name in the Project Explorer and create a new Javascript file, say index.js. By default, this is created in the WebContent subdirectory. This simple Javascript file has a function to add two numbers and it displays the result in a popup dialog:

function add_numbers(a, b) {
    return a + b;

alert("2 + 3 => " + add_numbers(2, 3));
  • Right-click on the project name in the Project Explorer and create a new HTML file, say index.html. By default, this is created in the root directory of the project. This simple HTML file loads the Javascript file we created above:
        <script type="text/javascript" src="index.js"></script>
  • We are all done! To run the Javascript code, we just need to open the index.html file in any browser. You can even do that from inside Eclipse: right-click on the file and choose Open With > Web Browser. You should be able to see the popup showing the sum of two numbers.

Tried with: Eclipse 3.8.1 and Ubuntu 14.04

5 thoughts on “How to program Javascript using Eclipse”

  1. i am working in the version: 4.2.2 i couldnt run in the way you have given. Could you please help me to sort out this problem


    1. Sharmila: Delete “WebContent” in the HTML code and it should work. It looks like recent versions of Eclipse will automatically refer to this directory.


  2. I can’t run the code by the way which you are given, please give the solution for that I’m using Eclipse kepler version. My problem is when i run the program the html file only runs,not the java script page.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s