Skip to main content

Posts

JavaScript Const

  The const keyword was introduced in ES6 (2015) . Variables defined with const cannot be Redeclared. Variables defined with const cannot be Reassigned. Variables defined with const have Block Scope. Cannot be Reassigned A const variable cannot be reassigned : Example const PI = 3.141592653589793 ; PI = 3.14 ;      // This will give an error PI = PI + 10 ;   // This will also give an error Must be Assigned JavaScript const variables must be assigned a value when they are declared: Correct const PI = 3.14159265359 ; Incorrect const PI; PI = 3.1 4159 265359 ; When to use JavaScript const? As a general rule, always declare a variable with const unless you know that the value will change. Use const when you declare: A new Array A new Object A new Function A new RegExp Constant Objects and Arrays The keyword const is a little misleading. It does not define a constant value. It def...
Recent posts

JavaScript Variables

  4 Ways to Declare a JavaScript Variable: Using var Using let Using const Using nothing   What are Variables? Variables are containers for storing data (storing data values). In this example, x , y , and z , are variables, declared with the var keyword: Example var x = 5 ; var y = 6 ; var z = x + y; In this example, x , y , and z , are variables, declared with the let keyword: Example let x = 5 ; let y = 6 ; let z = x + y; In this example, x , y , and z , are undeclared variables: Example x = 5 ; y = 6 ; z = x + y; From all the examples above, you can guess: x stores the value 5 y stores the value 6 z stores the value 11 When to Use JavaScript var? Always declare JavaScript variables with var , let , or const . The var keyword is used in all JavaScript code from 1995 to 2015. The let and const keywords were added to JavaScript in 2015. If you want your code to run in older browser, you must use var . When to Use JavaScri...

JavaScript Let

    The let keyword was introduced in ES6 (2015) . Variables defined with let cannot be Redeclared. Variables defined with let must be Declared before use. Variables defined with let have Block Scope. Cannot be Redeclared Variables defined with let cannot be redeclared . You cannot accidentally redeclare a variable. With let you can not do this: Example let x = "John Doe" ; let x = 0 ; // SyntaxError: 'x' has already been declared With var you can: Example var x = "John Doe" ; var x = 0 ; Block Scope Before ES6 (2015), JavaScript had only Global Scope and Function Scope . ES6 introduced two important new JavaScript keywords: let and const . These two keywords provide Block Scope in JavaScript. Variables declared inside a { } block cannot be accessed from outside the block: Example {    let x = 2 ; } // x can NOT be used here Variables declared with the var keyword can NOT have block sco...

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 executed). This example uses a single-line comment before each code line: Example // Change heading: document. getElementById ( "myH" ). innerHTML = "My First Page" ; // Change paragraph: document. getElementById ( "myP" ). innerHTML = "My first paragraph." ; This example uses a single line comment at the end of each line to explain the code: Example let x = 5 ;      // Declare x, give it the value of 5 let y = x + 2 ;  // Declare y, give it the value of x + 2 Multi-line Comments Multi-line comments start with /* and end with */ . Any text between /* and */ will be ignored by JavaS...

JavaScript Syntax

    JavaScript syntax is the set of rules, how JavaScript programs are constructed: // How to create variables: var x; let y; // How to use variables: x = 5 ; y = 6 ; let z = x + y; JavaScript Values The JavaScript syntax defines two types of values: Fixed values Variable values Fixed values are called Literals . Variable values are called Variables . JavaScript Literals The two most important syntax rules for fixed values are: 1. Numbers are written with or without decimals: 10. 50 1001 2. Strings are text, written within double or single quotes: "John Doe" 'John Doe' ADVERTISEMENT JavaScript Variables In a programming language, variables are used to store data values. JavaScript uses the keywords var , let and const to declare variables. An equal sign is used to assign values to variables. In this example, x is defined as a variable. Then, x is assigned (given) the value 6: let x; x = 6 ...

JavaScript Statements

    Example let x, y, z;    // Statement 1 x = 5 ;          // Statement 2 y = 6 ;          // Statement 3 z = x + y;      // Statement 4 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 HTML, JavaScript programs are executed by the web browser. JavaScript Statements JavaScript statements are composed of: Values, Operators, Expressions, Keywords, and Comments. This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo": Example document. getElementById ( "demo" ). innerHTML = "Hello Dolly." ; Most JavaScript programs contain many JavaScript statements. The statements are executed, ...

JavaScript Output

  JS 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: Example < !DOCTYPE html > < html > < body > < h1 > My First Web Page < /h1 > < p > My First Paragraph < /p > < p id ="demo" > < /p > < script > document. getElementById ( "demo" ). innerHTML = 5 + 6 ; < /script > < /body > < /html > Changing the innerHTML property of an HTML element is a common way to display data in HTML. Using document.wr...