Basic javascript grammar 1

Keywords: Javascript C

Four forms of declaration:

<script type="text/javascript">
    alert(1);
</script>
<a href="javascript:alert(2);">Eject</a>
<script scr="xxx.js"></script>
<div onclick="alert(3)";>Point me</div>

Four ways of displaying data:

1. alert() function bullet window warning
2. console.log print function (console output 100)
3. document.write(), which writes data to the browser

  <script type="text/javascript">
    console.log(100);
    document.write(100);
  </script>

4. Write to HTML elements using innerHTML

<body>
<h1>My first Web page</h1>
<p id="demo">My first paragraph</p>
<script>
document.getElementById("demo").innerHTML = "Paragraphs have been revised.";
</script>
</body>

Basic grammar:

1. Statements usually end with semicolons, if not
2. Naming rules:

Data type int float string array bool object
 Naming rule iAbc fAbc sAbc aAbc bAbc oAbc

3. Three Data Types

  • Objects (number, string, boolean, object)
var a = new Number(200);
var b = new String("abc");
var c = new Boolean(true);
  • undefined
var a;//type(a)=undefined
  • NaN(not a number)
var a = new Number("abc");
if (isNaN(a))
{
	alert("yes!")
}

NaN is generated when a string that is not a number is converted to a number

4. String begins with a number and gets its value through the corresponding function.

var a = '100abc'
var b = 'abc100'
var c = parseInt(a)
var d = parseInt(b)
alert(c)//c = 100
alert(d)//d = NaN

5. Functions:

<script>
function myFunction()
{
    alert("Hello World!");
}
function myFunction(a,b)
{
    return a*b;
}
var add = (function(m) {
    alert(m);
})(200);//Implementing immediate function execution
 

If... else if... The else statement:

if (condition1)
{
   //
}
else if (condition2)
{
    //
}
else
{
  //
}

case statement:

<button onclick="myFunction()">click here</button>
<p id="demo"></p>
<script>
function myFunction()
{
	var x;
	var d=new Date().getDay();
	switch (d)
    {
  		case 6:x="Today is Saturday.";
    	break;
  		case 0:x="Today is Sunday.";
    	break;
  		default:
    	x="Looking forward to the weekend";
  	}
	document.getElementById("demo").innerHTML=x;
}
</script>

for cycle:

//for cycle
for (var i=0;i<5;i++){
		alert(i);
	}
//for in cycle
function myFunction(){
	var x;
	var txt="";
	var person={fname:"Bill",lname:"Gates",age:56}; 
	for (x in person){
		txt=txt + person[x];
	}
	document.getElementById("demo").innerHTML=txt;
	//txt=BillGates56
}

Get parameters

def test()
{
	alert(arguments[0]);//Pop up 1
}
test(1,2,3,4)

while Loop

while (condition)
{
    Code to be executed
}

do-while Loop

do
{
    Code to be executed
}
while (condition);

break–continue:

Function in the circulatory system is the same as c language.

<script>
//break labelname; continue labelname; represents jumping out of the code segment
cars=["BMW","Volvo","Saab","Ford"];
list:{
	document.write(cars[0] + "<br>"); 
	document.write(cars[1] + "<br>"); 
	break list;
	document.write(cars[3] + "<br>"); 
	//BMW Volvo
}
</script>

Posted by Trekx on Mon, 01 Apr 2019 21:42:30 -0700