Basic javascript grammar - 01

Keywords: Web Development Javascript Attribute Java Programming

javascript basic grammar
Event handling in javascript
Implementing form validation using javascript (emphasis)

Introduction to JavaScript
JavaScript is a programming language embedded in HTML grammar to realize dynamic effect processing of pages.

javascript basic grammar (emphasis)

First, JavaScript is embedded in HTML grammar, so all code is marked with "<script>", but there are many kinds of scripts.
The script types used today are marked with the "type" attribute: "type="text/javascript". But "langusge" was also used at the earliest time.
Attribute annotations, such as "language="javascript ", are essentially the same, regardless of which one is used.


//Example: Defining scripts
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
alert("hello");//Warning box
alert("Hello");//Warning box
</script>
</body>
</html>

But there's a small problem here. If JavaScript often writes many lines of code in a project, then if it's all written in
In html, which can cause file confusion, JavaScript code is defined in a * js (script file) in many cases. Then
Use src attributes to import where needed

Example: Import files where needed
<html>
<head>
<title>JavaScript applications </title>
</head>
<body>
<! - Defines a script program - >.
<script type="text/javascript" src="demo.js">
</script>
</body>
</html>

Such a program is very easy to maintain.
In addition to using alert() for the output of the warning box pop-up, you can also use document.write() for page output.

Example "Page Output"
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
document.write("<h1>hello</h1>")
document.write("<h1>Hello</h1>")
</script>
</body>
</html>

Through the program, we can find that using JavaScript can output HTML code, and the output HTML code can also be parsed by browsers.
Understanding the basic structure, then we can enter into the learning of the program, mainly variables, functions, arrays.

1. Defining variables

JavaScript grammar is not very strict, unlike Java, when defining variables, you have to set the type of variables and prepare them at the same time.
Many keywords (int,double,byte, etc.) are uniformly represented in JavaScript by var keywords if you want to define variables, and
When defining variables, we do not know the specific types in them. The specific types need to be determined by the data set up again.

Example: Defining variables
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
var x;//Set variables, the type of variables is unknown
x=10;//Because an integer is set, the type of x is an integer.
alert(x+100);
</script>
</body>
</html>

Example: Defining strings
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
var x;//Set variables, the type of variables is unknown
x="hello";//Because of string type
alert(x+100);
</script>
</body>
</html>

And as browser versions improve, you can declare variables without var
<html>
<head>
<title>JavaScript applications </title>
</head>
<body>
<! - Defines a script program - >.
<script type="text/javascript" >
x="hello";
y=100;
alert(x+y);
</script>
</body>
</html>

Although this method is more convenient, it may also result in the following situation

Example: Observational issues
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
var name="zhangsan";//Variables can also be used directly without declaration, so there are no grammatical errors
Name="lisi";//Want to modify the name content
alert(name);
</script>
</body>
</html>

 

This requires you to pay attention to the naming criteria when writing JavaScript variables, the first word in lowercase, and then each word in lowercase.
Capital letters.

2. Procedural logic

There are three kinds of program logic: sequence, selection, and loop structure, which operate in the same way as before.


//Example: Write loop operations and generate tables
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
var lines=9;//Row number
var columns=5;//Column number
document.write("<table border=\"1\">");
for(var x=0;x<lines;x++){//Control line
document.write("<tr>");
for(var y=0;y<columns;y++){//Control column
document.write("<td>hello</td>");
}
document.write("<tr>");
}
document.write("</table>");
</script>
</body>
</html>

Example: 99 multiplication table
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
document.write("<table border=\"1\">");
for(var x=1;x<=9;x++){//Control line
document.write("<tr>");
for(var y=1;y<=x;y++){//Control column
document.write("<td>"+x+"+"+y+"="+x*y+"</td>");
}
for(var y=1;y<=9-x;y++){
document.write("<td>&nbsp</td>")
}
document.write("<tr>");
}
document.write("</table>");
</script>
</body>
</html>

3. Defining Functions

Defining methods in Java
[public|protected|private][static][final][synchronized] method name return value type (parameter type variable,. | variable)
[throws exception, exception] {
[return [return value];]
}
But defining functions in JavaScript is very simple.
Function function name (parameter,...)
{[return [return value];]
}
All functions are defined using function, and there is no declaration of return value type at the function declaration. If data needs to be returned, it is written directly.
The return syntax returns


//Example: Defining Functions
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
function add(x,y){
return x+y;}
alert(add(10,20));
alert(add("hello","world"));
</script>
</body>
</html>

The definition of function is so unprincipled, and the data type is completely arbitrary. Most importantly, when passing parameters, it can not be passed as required.
<html>
<head>
<title>JavaScript applications </title>
</head>
<body>
<! - Defines a script program - >.
<script type="text/javascript" >
function add(x,y){
return x+y;}
alert(add(10,20,30));
alert(add(10));
alert(add("hello","world"));
</script>
</body>
</html>

Although the parameters passed are very flexible, for the sake of the rigor of the program, the number of parameters is defined and the number of parameters is transferred.

4. Define arrays

Example: in JavaScript Defined array
<html>
<head>
<title>JavaScript application</title>
</head>
<body>
<!--Represents defining a script program-->
<script type="text/javascript" >
var sum=0;
var data=new Array(1,2,3); //An array that defines three elements
for(var x=0;x<data.length;x++){
sum+=data[x];
}
alert(sum);
</script>
</body>
</html

 

 

 

 

 

 

 

 

 

 

 

 

 

Posted by JCScoobyRS on Sat, 19 Jan 2019 01:18:12 -0800