JavaScript

lenguaje de programacion

JavaScript is a high-level programming language. It was originally designed as a scripting language for websites but became widely adopted as a general-purpose programming language, and is currently the most popular programming language in use[1]. JavaScript is usually found running in a web browser as interactive or automated content, ranging from popup messages and live clocks to large web applications. JavaScript is also commonly used in server-side programming through platforms like Node.js[2], or "embedded" in non-JavaScript applications where the base programming language lacks the high-level functionality that JavaScript offers.

Despite the similarities in name and syntax, JavaScript is not related to the programming language Java. Though the names of both languages are trademarks of Oracle Corporation, the two languages follow different design principles, and are actively developed by unrelated organizations.

SyntaxEdit

A JavaScript program is made of a collection of instructions called "statements". A semicolon marks the end of a statement, and allows multiple statements to be placed on the same line. However, it is typical to write each statement on its own line to keep a program file readable.

Variables can be defined in several ways. In an older version named "ES5", variables are defined using the var keyword.[3] In the newer versions after ES5, variables can be defined using const for constant variables and let for local variables.[4][5] 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;
3 
4 // ES6+
5 const y = 10;
6 let t = 5;

ExamplesEdit

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.[6]

 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();
 8 
 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  */
15 
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++){
 3 
 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 */
11 
12 }
13 
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() {
    alert("Hi!");
}

sayBye = function() {
    alert("Bye!");
}

sayHi();
sayBye();
  • 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

ReferencesEdit

  1. "Stack Overflow Developer Survey 2019". Stack Overflow. Retrieved 2020-03-11.
  2. "Server-Side Javascript: Back With a Vengeance". ReadWrite. 2009-12-17. Retrieved 2020-03-11.
  3. "var". MDN Web Docs. Retrieved 2018-03-24.
  4. "const". MDN Web Docs. Retrieved 2018-03-24.
  5. "let". MDN Web Docs. Retrieved 2018-03-24.
  6. [1]

External linksEdit