JavaScript Arithmetic

JavaScript Arithmetic Operators Arithmetic operators perform arithmetic on numbers (literals or variables). Operator Description + Addition – Subtraction * Multiplication / Division % Modulus (Remainder) ++ Increment — Decrement Arithmetic Operations A typical arithmetic operation operates on two numbers. The two numbers can be literals: Example var x = 100 + 50; Try it Yourself » or variables: Example […]

JavaScript Operators

Example Assign values to variables and add them together: var x = 5;         // assign the value 5 to x var y = 2;         // assign the value 2 to y var z = x + y;     // assign the value 7 to z (x + y) Try it Yourself » The assignment operator (=) assigns a value to a variable. Assignment var x = 10; Try […]

JavaScript Variables

JavaScript variables are containers for storing data values. In this example, x, y, and z, are variables: Example var x = 5; var y = 6; var z = x + y; Try it Yourself » From the example above, you can expect: x stores the value 5 y stores the value 6 z stores the value 11 Much Like […]

JavaScript Comments

JavaScript comments can be used to explain JavaScript code, and to make it more readable. JavaScript comments can also be used to prevent execution, when testing alternative code. Single Line Comments Single line comments start with //. Any text between // and the end of the line will be ignored by JavaScript (will not be […]

JavaScript Syntax

JavaScript syntax is the set of rules, how JavaScript programs are constructed: var x, y;          // How to declare variables x = 5; y = 6;      // How to assign values z = x + y;         // How to compute values JavaScript Values The JavaScript syntax defines two types of values: Fixed values and variable values. Fixed values are called literals. Variable values […]

JavaScript Statements

Example var x, y, z;    // Statement 1 x = 5;          // Statement 2 y = 6;          // Statement 3 z = x + y;      // Statement 4 Try it Yourself » JavaScript Programs A computer program is a list of “instructions” to be “executed” by a computer. In a programming language, these programming instructions are called statements. A JavaScript program is a list of programming statements. In […]

JavaScript Output

JavaScript Display Possibilities JavaScript can “display” data in different ways: Writing into an HTML element, using innerHTML. Writing into the HTML output using document.write(). Writing into an alert box, using window.alert(). Writing into the browser console, using console.log(). Using innerHTML To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content: […]

JavaScript Where To

The <script> Tag In HTML, JavaScript code must be inserted between <script> and </script> tags. Example <script> document.getElementById(“demo”).innerHTML = “My First JavaScript”; </script> Try it Yourself » Old JavaScript examples may use a type attribute: <script type=”text/javascript”>. The type attribute is not required. JavaScript is the default scripting language in HTML. JavaScript Functions and Events A JavaScript function is […]

JavaScript Introduction

This page contains some examples of what JavaScript can do. JavaScript Can Change HTML Content One of many JavaScript HTML methods is getElementById(). This example uses the method to “find” an HTML element (with id=”demo”) and changes the element content (innerHTML) to “Hello JavaScript”: document.getElementById(“demo”).innerHTML = “Hello JavaScript”; Try it Yourself » JavaScript accepts both double and single […]