Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 72 - 2609_Logicode
P. 72

    Understanding  the components  of a JavaScript statement  is essential  for  writing functional
                      code and building interactive web applications.

                      VARIABLES
                      A variable is a memory reference to a stored value. It is used to name any data so that it can be

                      accessed later in the script. Variables can be declared and initialised in a script as follows:
                         let: It declares a variable that can be changed later. It’s limited to the block it’s in (like a loop or
                        function).

                        let x = 5; // Here, you can change the value of variable x.
                        x = 10;

                         var: It declares a variable that can be changed and redeclared. It works globally or within a
                        function, but it’s less commonly used now.

                        var y = 20;

                        var y = 30;  // You can redeclare it.
                        Here, you can redeclare the value of variable y.
                         const: It declares a constant that can’t be changed. Once a value is set, it stays the same.

                        const z = 15;

                        z = 20;  // Error! You can't change a const value.
                        Here, the error occurs. You cannot change the value of a const variable.
                      When naming variables, the following rules must be followed:

                         A variable can contain letters, numbers, dollar signs ($) and underscores (_).
                         Spaces are not allowed in variable names.

                         JavaScript keywords cannot be used as variable names.
                          A variable name cannot start with a numeral (0-9). It must begin with a letter or an underscore.
                         For example, 123test is invalid, but _123test is valid.

                         Variable names are case-sensitive. For example, Name and name are different variables.
                         Variable names must be shorter than 255 characters.

                         Code
                           3       Create a web page to demonstrate the use of variables in JavaScript.

                      <!DOCTYPE HTML>
                      <HTML>

                      <HEAD>
                      <TITLE>Variables</TITLE>

                      </HEAD>

                      <BODY>

                       70   Premium Edition-VIII

| 67 68 69 70 71 72 73 74 75 76 77 |