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.
Separate JavaScript statements with semicolons (;).
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JavaScript example</title>
- </head>
- <body>
- <h1>JavaCrypt statements are separated by semicolons.</h1>
- <p id="test"></p>
- <script>
- a = 20;
- b = 30;
- c = a + b;
- document.getElementById("test").innerHTML = c;
- </script>
- </body>
- </html>
Output:
JavaCrypt statements are separated by semicolons.
50
HTML 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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>JavaScript example</title>
- </head>
- <body>
- <h1>JavaScript example.</h1>
- <p id="test"></p>
- <script>
- var a;
- a = 20;
- document.getElementById("test").innerHTML = a;
- </script>
- </body>
- </html>
Output:
JavaScript example.
20
JavaScript operator:
JavaScript typically uses the assignment operator (=) to keep values within variables:
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>operator (=) </title>
- </head>
- <body>
- <h1>operator (=) .</h1>
- <p id="test"></p>
- <script>
- var a;
- a = 50;
- document.getElementById("test").innerHTML = a;
- </script>
- </body>
- </html>
Output:
operator (=) .
50
The mathematical operator (+ - * /) is commonly used in JavaScript to determine values:
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>The emphasis is on multiplication before adding.</h1>
- <h1>But before multiplication, the first bracket is done.</h1>
- <p id="test"></p>
- <script>
- document.getElementById("test").innerHTML = (20 + 10) * 3;
- </script>
- </body>
- </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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>Value determination through expression.</h1>
- <p id="test"></p>
- <script>
- document.getElementById("test").innerHTML = 40 + 60;
- </script>
- </body>
- </html>
Output:
Value determination through expression.
100
Many times expressions can have variables as values:
Example:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>Value determination through expression.</h1>
- <p id="test"></p>
- <script>
- var a=30;
- document.getElementById("test").innerHTML = a + 20;
- </script>
- </body>
- </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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>Value determination through expression.</h1>
- <p id="test"></p>
- <script>
- document.getElementById("test").innerHTML = "Mamun" + " " + "Khan";
- </script>
- </body>
- </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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>var creates the keyword variable.</h1>
- <p id="test"></p>
- <script>
- var a = 10 + 40;
- var b = a * 5;
- document.getElementById("test").innerHTML = b;
- </script>
- </body>
- </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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>It will not be edited.</h1>
- <p id="test"></p>
- <script>
//var a = 10 + 40; It will not be edited.
var b = 5 * 5;
document.getElementById("test").innerHTML = b;
</script> - </body>
- </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.
The 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:
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>This is JavaScript Example !</title>
- </head>
- <body>
- <h1>Try changing the lastName to lastname..</h1>
- <p id="test"></p>
- <script>
- var firstName = "Arfin";
- var firstname = "Mamun";
- document.getElementById("test").innerHTML =firstName;
- </script>
- </body>
- </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.
Hyphen (-) cannot be used in JavaScript. JavaScript treats hyphens as minus signs.
shelleyakter
wow nice post
shelleyakter
nice post