JavaScript基本语法介绍和用法指南

2021年3月26日17:29:00 发表评论 663 次浏览

JavaScript是轻量级的动态计算机编程语言。它用于创建客户端动态页面。它是开源和跨平台语言。

基本语法

<script>  
document.write( "Basic Print method in JavaScript" );  
</script>

JavaScript语法指的是确定如何构造JavaScript程序的规则集:

// Variable declaration
var c, d, e;

// Assign value to the variable
c = 5; 

// Computer value of variables
d = c;
e = c/d;

JavaScript变量:JavaScript变量是要存储数据的存储位置的简单名称。 JavaScript中有以下两种类型的变量:

  • 局部变量:在块或函数内部声明一个变量。
  • 全局变量:在函数外部或与窗口对象一起声明变量。

例子:

<script>
  
// Declare a variable and initialize it
// Gloabal variable declaration
var Name= "Apple" ;   
  
// Function definition
function MyFunction() {  
      
     // Local variable declaration
     var num = 45;    
      
     // Display the value of Gloabal variable
     document.writeln(Name);
      
     // Display the value of local variable
     document.writeln( "<br>" + num );
}  
  
// Function call
MyFunction();
   
</script>

输出如下:

Apple
45

JavaScript运算子:JavaScript运算符是用于计算值的符号, 换句话说, 我们可以对操作数执行操作。算术运算符(+, -, *, /)用于计算值, 赋值运算符(=, + =, %=)用于将值分配给变量。

例子:

<script> 
  
// Variable Declarations
var x, y, sum;
  
// Assign value to the variables
x = 3;
y = 23;
  
// Use arithmetic operator to
// add two numbers
sum = x + y;
  
document.write(sum);
  
</script>

输出如下:

26

JavaScript表达式:表达式是值, 运算符和变量的组合。它用于计算值。

例子:

<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
// Expression to divide a number
num = x / 2;
  
// Expression to add two numbers
sum = x + y;
  
document.write(num + "<br>" + sum);
  
</script>

输出如下:

10
50

JavaScript关键字:关键字是保留字, 在JavaScript中具有特殊含义。

// var is the keyword used to define the variable
var a, b;

// function is the keyword which tells the browser to create a function
function GFG(){};

JavaScript注释:JavaScript编译器将忽略这些注释。它增加了代码的可读性。它添加了建议, 信息和代码警告。在双斜杠//(单行注释)之后或/ *和* /(多行注释)之间写的任何内容均被视为注释, 并被JavaScript编译器忽略。

例子:

<script> 
  
// Variable Declarations
var x, num, sum;
  
// Assign value to the variables
x = 20;
y = 30
  
/* Expression to add two numbers */
sum = x + y;
  
document.write(sum);
  
</script>
50

JavaScript数据类型:JavaScript提供了不同的数据类型来保存变量的不同值。 JavaScript是一种动态编程语言, 它意味着不需要指定变量的类型。 JavaScript中有两种类型的数据类型。

  • 原始数据类型
  • 非原始(参考)数据类型
// It store string data type
var txt = "lsbin";

// It store integer data type
var a = 5;
var b = 5;

// It store Boolean data type
(a == b )
 
// It store array data type
var places= ["GFG", "Computer", "Hello"];

// It store object data
var Stdent = {firstName:"Johnny", lastName:"Diaz", age:35, mark:"blueEYE"}

JavaScript函数:JavaScript函数是用于执行某些特定操作的代码块。当有人调用它时, 将执行JavaScript函数。它会多次调用, 因此该函数可重用。

语法如下:

function functionName( par1, par2, ....., parn ) {  
    // Function code
}

JavaScript函数可以包含零个或多个参数。

例子:

<script>
    
// Function definition
function func() {  
      
     // Declare a variable
     var num = 45;                  
      
     // Display the result
     document.writeln(num);     
}  
  
// Function call
func();
  
</script>

输出如下:

45

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: