js基础语法小全

版权声明:转载声明原作者及链接 https://blog.csdn.net/ICY___/article/details/85101573

js,作为前端中的“c“,在圈中的地位是不言而喻的,和每一门编程语言一样,它们之间都有大致相同的语法。

  • js的历史及其作用

为啥叫JavaScript,因为跟风,和现在蹭流量蹭热度类似吧,JavaScript和Java其实并没有太大的联系。

作用,和html css 一样重要,它是整个项目的灵魂!

  • 引入方法{

内部,外部
大致和css相同,但引入的位置不同效果也不同


基本语法

  • 定义变量:

var
输出:
console.log()

var a=10;
var b='nihao'
var c=null;
var d=false;
var e=[1,2,3,4,5];
console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);
function hansu (a,b){
    var name='zhangsan';
    console.log(name);
    console.log(a+b)
}
hansu(1,3);
  • 函数

function 函数名称(形参){
具体内容
}

  • 函数的实现;

首先需要将变量声明
函数名称 (实参)

// first 常规函数(传参)
function students (name,sex,age){
    console.log(name);
    console.log(sex);
    console.log(age);
}
students('zhangliang','man',20);
// 不传参型
var a='what time is it?'
var b='what is you name?'
function words (){
    console.log(a);
}
words();


// second 匿名函数一(不传参)
var things=function (){
    console.log(b);
}
things();

// 传参
var content=function(text){
    console.log(text);
}
content(12345);


// 匿名函数二(真正的匿名函数,自己执行,原理是函数一的合成)不传参
(function (){
    var p=20;
    console.log(p)
})();

// 传参
(function (n,m,l){
    console.log(n,m,l)
}
)(7,8,9);
// 返回值(函数输出时可以附加返回值,需要打印函数执行后的结果)
function students (name,sex,age){
    console.log(name);
    console.log(sex);
    console.log(age);
    return 20;
}
console.log(students());
  • 全局变量和局部变量

相对概念;
局部变量与全局变量互不相干

  • 操作运算符

无非就是加减乘除,自加/自减,大于小于,等于不等于之类,与或非,三元运算符。

看代码更容易理解

// 运算符
// 算术运算符{
//     + - * / % ++ --
// }
var a=10.3;
var b=20;
console.log((a++)+b);
console.log(a);
// 强制转换
parseInt(a);//这只是一种展示方式,对于数据本生没有改变。
console.log(parseInt(a));
// ++   --   在前还是在后问题: 前置为先计算,后执行。后置为先执行,后运算;

// 比较运算符
// >  <  >=  <=  ==(值等于) ===(完全等于) !=
console.log(a!=b);
var name='66';
var c=66;
console.log(name===c);
console.log(name==c);

// 逻辑运算符   &&   ||    !
var text1=2;
var text2=4;
if(text1==3&&text2==4){
    console.log("1")
}
else console.log("2");

// 三元运算符    判断条件 ? true:false
text1>text2 ?console.log('true'):console.log('false');
  • 循环结构

1.for     2.do while    3.while

个人认为do while  和while并无太大的区别,只是do while至少执行一次循环中的语句而已。

​
// 结构体系 循环结构
// for   do while   while
// do while先执行后判断,至少执行一次
// 基本结构
// for(i=0;i<10;i++){
//     console.log(i);
// }
// var n=0;
//  do{
//     n++;
//      console.log(n);

//  }while(n<10)
// while(n<10){
//     n++;
//     console.log(n);
// }
// return(只能用于函数,直接跳出)    break(跳出当前循环)     continue(跳过本次循环)


​
  • 关于循环的练习案例
// 敲星星
/*  ********
    ********
    ********
    ********
    ********   */
for (i = 0; i < 5; i++) {
    document.write('********');
    document.write('<br>');
}
/*  *
    **
    ***
    ****
    *****   */
for (i = 0; i < 5; i++) {
    document.write('<br>')
    for (j = 1; j <= i + 1; j++) {
        document.write('*')
    }
}

document.write('<br>');
document.write('<br>');
/*   ********
    ********
   ********
  ********
 ********   */
for (i = 0; i < 5; i++) {
    for (j = 0; j < 5 - i; j++) {
        document.write('&nbsp');
    }
    document.write('********');
    document.write('<br>');
}
/*99乘法表*/
for (i = 1; i <= 9; i++) {
    for (j = 1; j <= i; j++) {
        document.write(i + '*' + j + '=' + (i * j >= 10 ? i * j + '&nbsp;&nbsp;' : i * j + '&nbsp;&nbsp;&nbsp;&nbsp;'));
    }
    document.write('<br>');
}
  • 注释方法

和c相同,//   为单行注释

/*   

*/ 为多行注释

猜你喜欢

转载自blog.csdn.net/ICY___/article/details/85101573
今日推荐