JavaScript Structure

JavaScript Structure

JavaScript Structure:

JavaScript program:

A computer program is a set of "instructions" that are performed by a computer.

In programming these instructions are called statements.

JavaScript is a programming language.

enlightenedSeparate JavaScript statements with semicolons (;).

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.    <title>JavaScript example</title>
  5.   </head>
  6.   <body>
  7.     <h1>JavaCrypt statements are separated by semicolons.</h1>
  8.     <p id="test"></p>
  9.  
  10.     <script>
  11.        a = 20;
  12.        b = 30;
  13.        c = a + b;
  14.        document.getElementById("test").innerHTML = c;
  15.     </script>
  16.       
  17.   </body>
  18. </html>

 

Output:

 

JavaCrypt statements are separated by semicolons.

50

 

enlightenedHTML JavaScript programs are executed through the browser.
 

JavaScript Statement:

JavaScript statements are usually made up of values, operators, expressions, keywords and comments.

JavaScript Value:

 

Two types of values are used in JavaScript. Namely- fixed value and variable value.

Fixed values are called literal. A variable value is called a variable.

JavaScript Literal:


The most important rule for writing a fixed value is:

Write the number as a whole number or a decimal:

 

var a = 140.24; // Numbers with decimals
var b = 140; // Numbers without decimals

 

String is text that is written in single or double quotation marks:

var a = "Manub";
var b = 'Hasib';

 

JavaScript variables:
In programming languages, variables are used to store values.

JavaScript uses the var keyword when declaring a variable.

The equals (=) symbol is used to hold values ​​in variables.

In the following example, a is first declared as a variable by the var keyword, then 10 is placed as a value in it using the equal sign.

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.    <title>JavaScript example</title>
  5.   </head>
  6.   <body>
  7.     <h1>JavaScript example.</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        var a;
  11.        a = 20;
  12.        document.getElementById("test").innerHTML = a;
  13.     </script>
  14.       
  15.   </body>
  16. </html>

 

Output:

    

JavaScript example.

   

20

       

 

JavaScript operator:

JavaScript typically uses the assignment operator (=) to keep values within variables:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.    <title>operator (=) </title>
  5.   </head>
  6.   <body>
  7.     <h1>operator (=) .</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        var a;
  11.        a = 50;
  12.        document.getElementById("test").innerHTML = a;
  13.     </script>
  14.       
  15.   </body>
  16. </html>

 

Output:

    

operator (=) .

   

50

 

 

The mathematical operator (+ - * /) is commonly used in JavaScript to determine values:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>The emphasis is on multiplication before adding.</h1>
  8.     <h1>But before multiplication, the first bracket is done.</h1>
  9.     <p id="test"></p>
  10.  
  11.     <script>
  12.        document.getElementById("test").innerHTML = (20 + 10) * 3;
  13.     </script>
  14.  
  15.   </body>
  16. </html>

 

Output:

 

The emphasis is on multiplication before adding.

But before multiplication, the first bracket is done.

    

90

    

 

JavaScript Expressions:
JavaScript expressions consist of a combination of some values, variables, and operators that determine a value.

For example, the product of 5 * 10 is converted to 50.

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>Value determination through expression.</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        document.getElementById("test").innerHTML = 40 + 60;
  11.     </script>
  12.   </body>
  13. </html>

 

Output:

           

Value determination through expression.

    

100

 

 

Many times expressions can have variables as values:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>Value determination through expression.</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        var a=30;
  11.        document.getElementById("test").innerHTML = a + 20;
  12.     </script>
  13.   </body>
  14. </html>

 

Output:

                 

Value determination through expression.

    

50

      

 

Values in JavaScript can be of different types. For example: numbers and strings.

For example, "Kamal" + " " + "Khan", plus "Kamal Khan" is:

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>Value determination through expression.</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        document.getElementById("test").innerHTML = "Mamun" + " " + "Khan";
  11.     </script>
  12.   </body>
  13. </html>

 

Output:

                 

Value determination through expression.

    

Mamun Khan

      

 

JavaScript Key Word:

JavaScript keywords are used to perform various tasks.

For example, var keyword tells the browser to create a variable.

Example:

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>var creates the keyword variable.</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        var a = 10 + 40;
  11.        var b = a * 5;
  12.        document.getElementById("test").innerHTML = b;
  13.     </script>
  14.   </body>
  15. </html>

 

Output:

                 

var creates the keyword variable.

    

250

      

 

JavaScript comment:

Not all JavaScript statements are edited.

The codes after the double slash (//) or between /* and * / are treated as comments.

The comment codes are ignored and will not be edited:

Example:

 

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>It will not be edited.</h1>
  8.     <p id="test"></p>
  9.     <script>
     //var a = 10 + 40; It will not be edited.
           var b = 5 * 5;
           document.getElementById("test").innerHTML = b;
        </script>
  10.      
  11.   </body>
  12. </html>

 

 

Output:

                 

It will not be edited.

    

25

      

 

JavaScript identifier:

Identifiers are names

Identifiers are used in JavaScript to name variables, keywords, and functions.

The naming rules are the same in most programs.

The first character in JavaScript must be a character, underscore (_) or dollar ($) sign.

Subsequent characters can be characters, numbers, underscores (_) or dollar ($) symbols.

enlightenedThe first letter will never be a number.
      In this way JavaScript can easily separate the identifier from the number.

 

JavaScript case sensitive:

All JavaScript identifiers are case-sensitive.

lastName and lastname are two completely different variables

 

Example:

 

  1. <html lang="en">
  2.   <head>
  3.     <meta charset="utf-8">
  4.     <title>This is JavaScript Example !</title>
  5.   </head>
  6.   <body>
  7.     <h1>Try changing the lastName to lastname..</h1>
  8.     <p id="test"></p>
  9.     <script>
  10.        var firstName = "Arfin";
  11.        var firstname = "Mamun";
  12.        document.getElementById("test").innerHTML =firstName;
  13.     </script>
  14.   </body>
  15. </html>

 

 

Output:

                 

Try changing the lastName to lastname..

    

Arfin

 

JavaScript does not consider VAR or Var as var keywords.

JavaScript Camel Case:

Programmers usually use three methods to write more than one word in one word:

 

Hyphen (-)

first-name, last-name, master-card, inter-city

Underscore (_)

first_name, last_name, master_card, inter_city

Camel case 

FirstName, LastName, MasterCard, InterCity

In programming languages, especially in JavaScript, the camel case begins with a lowercase letter:

FirstName, LastName, MasterCard, InterCity.

enlightenedHyphen (-) cannot be used in JavaScript. JavaScript treats hyphens as minus signs.

 

 

Thank You.............