JavaScript Hoisting

Hoisting is JavaScript’s default behavior of moving declarations to the top. JavaScript Declarations are Hoisted In JavaScript, a variable can be declared after it has been used. In other words; a variable can be used before it has been declared. Example 1 gives the same result as Example 2: Example 1 x = 5; // Assign 5 to x […]

JavaScript Debugging

Errors can (will) happen, every time you write some new computer code. Code Debugging Programming code might contain syntax errors, or logical errors. Many of these errors are difficult to diagnose. Often, when programming code contains errors, nothing will happen. There are no error messages, and you will get no indications where to search for […]

JavaScript Type Conversion

Number() converts to a Number, String() converts to a String, Boolean() converts to a Boolean. JavaScript Data Types In JavaScript there are 5 different data types that can contain values: string number boolean object function There are 3 types of objects: Object Date Array And 2 data types that cannot contain values: null undefined The […]

JavaScript Booleans

A JavaScript Boolean represents one of two values: true or false. Boolean Values Very often, in programming, you will need a data type that can only have one of two values, like YES / NO ON / OFF TRUE / FALSE For this, JavaScript has a Boolean data type. It can only take the values true or false. The Boolean() Function You can […]

JavaScript Math Object

The JavaScript Math object allows you to perform mathematical tasks on numbers. Example Math.PI;            // returns 3.141592653589793 Try it Yourself » Math.round() Math.round(x) returns the value of x rounded to its nearest integer: Example Math.round(4.7);    // returns 5 Math.round(4.4);    // returns 4 Try it Yourself » Math.pow() Math.pow(x, y) returns the value of x to the power of […]

JavaScript Set Date Methods

Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object. Set Date Methods Set Date methods are used for setting a part of a date: Method Description setDate() Set the day as a number (1-31) setFullYear() Set the year (optionally month and day) setHours() Set the […]

JavaScript Strings

JavaScript strings are used for storing and manipulating text. JavaScript Strings A JavaScript string is zero or more characters written inside quotes. Example var answer = “It’s alright”; var answer = “He is called ‘Johnny’”; var answer = ‘He is called “Johnny”‘; Try it Yourself » String Length The length of a string is found in the built in property length: Example […]

JavaScript Functions

A JavaScript function is a block of code designed to perform a particular task. A JavaScript function is executed when “something” invokes it (calls it). Example function myFunction(p1, p2) { return p1 * p2;              // The function returns the product of p1 and p2 } Try it Yourself » JavaScript Function Syntax A JavaScript function is defined […]

JavaScript Assignment

JavaScript Assignment Operators Assignment operators assign values to JavaScript variables. Operator Example Same As = x = y x = y += x += y x = x + y -= x -= y x = x – y *= x *= y x = x * y /= x /= y x = x […]

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: […]