JavaScript基础(二)——基础语法与函数基础

一、变量与数据类型

1、结尾的分号可加可不加。

2、变量名可以由数字、字母、下划线、$组成,但是不能以数字开头。

3、六种数据类型:

数值(Number):1,2,3...

字符串(String):“hello world”

布尔(Boolean):true、false

未定义(Undefined):undefined

空(Null):null

对象(Object):{}

二、表达式与运算符

1、比较运算符

20 == “20”    :    true(不比较数据类型,内部会自动进行类型转换)

20 === “20”    :    false(比较数据类型,并且比较值

20 != "20"     :    false

20 !== "20"    :   true

三、条件语句

1、switch语句

    <script>
        switch (op) {
            case "+":
                console.log(num1 + num2);
                break;
            case "-":
                console.log(num1 - num2);
                break;
            case "*":
                console.log(num1 * num2);
                break;
            case "/":
                console.log(num1 / num2);
                break;
            default:
                console.log("请输入正确的操作符");
        }
    </script>

2、三目运算符 

    <script>
        num1 > num2 ? console.log("num1大于num2") : console.log("num1小于或等于num2");
    </script>

分析:

num1>num2吗?

是的话,执行前半句,不是的话,执行后半句。 

四、函数

 1、函数的声明与调用

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo02</title>
</head>

<body>
    <script>
        function fun() {
            console.log("hello world");
        }

        fun();
    </script>
</body>

</html>

效果如下:

分析:

先声明函数fun(),然后再调用函数fun()。

2、函数的传参

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo02</title>
</head>

<body>
    <script>
        function fun(x, y) {
            var result = x * x + y * y;
            console.log(result);
        }

        fun(3, 4);
    </script>
</body>

</html>

效果如下:

分析:

fun(3,4),就是将3作为x的值,4作为y的值,然后进入函数计算。 

代入,得:result  =  3 * 3 + 4 * 4 = 25;

 3、函数的返回值(return)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo02</title>
</head>

<body>
    <script>
        function fun(x, y) {
            var result = x * x + y * y;
            return result;
        }

        console.log(fun(3, 4));
    </script>
</body>

</html>

效果如下:

分析:

函数的返回值的意思就是:当程序员调用函数时,这个函数就相当于一个表达式,它有它的返回值,返回值即为return后面的值。 

 4、示意图

5、案例——判断用户登录

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Demo02</title>
</head>

<body>
    <script>
        function judge(username, password) {
            if (username === "admin" && password === "123456") {
                console.log("登陆成功!");
            } else {
                console.log("登陆失败");
            }
        }

        judge("xiaoming", "123456");
        judge("admin", "123");
        judge("admin", "123456");
    </script>
</body>

</html>

效果如下:

分析:

只有当用户名和密码都正确时,才登陆成功。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pro1822

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值