programming language
(Redirected from Javascript)

JavaScript is a high-level, interpreted programming language for computers. It is often run in web browser applications to create things that work by themselves like a popup message or a live clock. It is not related to the programming language Java.

Although Java and JavaScript have many external similarities such as name and syntax, the two languages follow different design principles.


Variables can be defined in several ways. In an older version named "ES5", variables are defined using the var keyword.[1] In the newer versions after ES5, variables can be defined using const for constant variables and let for local variables.[2][3] The value of constant variables cannot be re-declared or reassigned. Variables assigned using const or let are contained within blocks, while variables assigned using var are contained within functions.

1 // ES5
2 var x = 1;
4 // ES6+
5 const y = 10;
6 let t = 5;


The script below prints "Example" on the screen. The lines that start with// are comments, which are used to describe the actions of the program.[4]

 1 function example()
 2 {
 3     var ex = document.createTextNode('Example'); //make the computer remember "Example",
 4                                                  //so whenever you say "ex" the computer will append it with "Example"
 5     document.body.appendChild(ex);               //put the text on the bottom of the webpage
 6 }
 7 example();
 9 /*
10  * The code below does almost the same thing as the code above,
11  * but it shows "Example" in a popup box and is shorter.
12  *
13  * This is a comment too, by the way.
14  */
16 alert("Example");

JavaScript in HTML is enclosed within <script></script> tags. The tags indicate that it is a script and not text to be put onto the web page. The script below inserts the numbers 1 through 10 at the bottom of a webpage:

 1 <script type="text/javascript">
 2 for(var numOfTimesAround = 1; numOfTimesAround <= 10; numOfTimesAround++){
 4 document.body.innerHTML = document.body.innerHTML + numOfTimesAround + "<br>";
 5 /*
 6 * This puts the number, then a new line, at the end of the web page.
 7 * In javascript, using the + sign combines two words together.
 8 * So writing "Hello" + " World" would make "Hello World".
 9 * Or, writing 1 + "<br>" makes "1<br>", which is what we want.
10 */
12 }
14 </script>

The for() loop makes whatever code is between the { and the } happen more than one time. In this case, it keeps looping until numOfTimesAround is equal to 10, then it stops.

Differences between Java and JavascriptEdit

  • In Java, to define a variable, you have to say what type of variable it is: a number, a word, a letter, or more. In JavaScript, this is not necessary.
  • In JavaScript, functions are stored as variables (unlike Java). This makes the following code okay in JavaScript:
function sayHi() {

sayHi = function() {

  • JavaScript is interpreted, but Java, in most cases, must be compiled. This means that JavaScript needs to be run by another computer program (an interpreter), but finished Java can be run as its own program.

Related pagesEdit


  1. "var". MDN Web Docs. Retrieved 2018-03-24.
  2. "const". MDN Web Docs. Retrieved 2018-03-24.
  3. "let". MDN Web Docs. Retrieved 2018-03-24.
  4. [1]

External linksEdit