JavaScript-1-基础、数据类型【注意:string!!!】

本文深入解析JavaScript的基础语法,涵盖ECMAScript语法、DOM及BOM模型,详细讲解变量声明、数据类型、运算符及控制台操作。通过实例演示字符串处理、数字运算及常见弹窗函数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本来没打算写这个的,结果写css写的很烦躁,啊!!!然后乖乖过来写这个了。。。。
焦躁的SH。。。QAQ。。。。

基础

  1. JavaScript包含:
    ECMAScript 基础语法
    DOM 文档对象模型
    BOM 浏览器对象模型

    详解

  2. 浏览器弹出框 alert() 和 prompt() 【两个都属于DOM???还是BOM??不确定。。后续补充】

    var a=prompt();
    alert(a);
    // console.log(a);  -- 控制台输出
    
  3. 控制台输出 console.log() console.warn() console.error()

    var a=prompt();
    console.log(); 正常
    console.warn(); 警告
    console.error(); 错误
    
  4. 变量名由 数字、字母、下划线、$ 组成

  5. 赋值

    /*  变量  x=undefined y=undefined z=10 */
    var x,y,z=10;
    console.log(x); /* undefined */
    console.log(y); /* undefined */
    console.log(z); /* 10 */
    

数据类型

  1. 6大基本数据类型

    number 【typeof:number】
    string【typeof:string】
    boolean【typeof:boolean】
    null【typeof:object】
    undefined【typeof:undefined】
    object【typeof:object】
    

    6 个 typeof:number、string、boolean、undefined、object、function

    undefined 未定义
    var a;
    alert(a); undefined
    alert(typeof a); undefined

  2. string

    == 为值相等
    === 为 值相等+类型相等

    js中的 ‘’ 和 String() 相同,为基本类型
    js中的 new String() 和上述不同,为引用类型

    var s1='hello 123asd';
    var s2="hello 123asd";
    var s3=String('hello 123asd');
    var s4=new String('hello 123asd');
    console.log(s1==s2); /* true */
    console.log(s1===s2); /* true */
    console.log(s1==s3); /* true */
    console.log(s1===s3); /* true */
    console.log(s1==s4); /* true */
    console.log(s1===s4); /* false */
    console.log(s1); /* hello 123asd */
    console.log(s2); /* hello 123asd */
    console.log(s3); /* hello 123asd */
    console.log(s4); /* [String: 'hello 123asd'] */
    console.log(typeof s1); /*string*/
    console.log(typeof s2); /*string*/
    console.log(typeof s3); /*string*/
    console.log(typeof s4); /*object*/
    

    java:
    a = “123 qwe”;
    b = “123 qwe”;
    c = new String(“123 qwe”);
    a 和 b的值相等,地址相同
    a 和 c的值相同,地址不同

运算符

(其他不细说了,与python去比的。。)

  1. ++ 和 – 为自增、自减 (python没有)

  2. == 和 ===
    == 值相等
    === 值相等 + 类型相同

    var a=1;
    var s='1';
    console.log(a==s); /* true */
    console.log(a===s); /* false */
    
  3. 注意 + 功能
    字符串连字符
    数字加法
    正号(+string ==> 正整数)

  4. js中没有整除,即 a/b 为浮点数

    ps:小例子数字反转

    直接用数字时,需要 parseInt(num/10);

    var n1=123456;
    var n1r=0;
    var i=5;
    while(n1>0){
        var tt=1;
        for (var j=1;j<=i;j++){
            tt*=10;
        }
        n1r+=n1%10*tt;
        n1=parseInt(n1/10);
        i-=1;
    }
    console.log(n1r);
    

    利用字符串

    var n2=123456;
    var s2=n2+'';
    var s2r='';
    for(var i=s2.length-1;i>=0;i--){
        s2r+=s2[i];
    }
    console.log(+s2r);
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值