首页 > 其他分享 >兼收并蓄 TypeScript - 基础: null, undefined

兼收并蓄 TypeScript - 基础: null, undefined

时间:2024-09-20 12:02:17浏览次数:1  
标签:TypeScript console undefined object webabcd null log

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 基础: null, undefined

示例如下:

basic\null_undefined.ts

{
    console.log(undefined == null, undefined === null); // true false
    console.log(typeof null, typeof undefined); // object undefined
    console.log(Number(null), Number(undefined)); // 0 NaN
    console.log(Boolean(null), Boolean(undefined)); // false false
}

{
    // 变量声明了,但是没有赋值,就是 undefined
    let a
    console.log(a); // undefined

    // 函数没有返回值,其返回的就是 undefined
    let b = () => {

    };
    console.log(b());  // undefined

    // 判断是否是 undefined
    console.log(a == undefined, typeof a == 'undefined');  // true true

    // 注:本例中 a 是 undefined,但是其数据类型是 any(可以赋值为任意基本类型或任意对象)
    a = 10;
    a = "abc";
    a = new Date();
}

{
    // 声明变量时,为其赋值为 null
    let a = null;
    // 值为 null 的变量的数据类型是 object
    console.log(typeof a);  // object

    // 注:本例中 a 是 null,但是其数据类型是 object(可以赋值为任意对象)
    a = new Date();
    a = new Array();
}

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:TypeScript,console,undefined,object,webabcd,null,log
From: https://www.cnblogs.com/webabcd/p/18422227/typescript_basic_null_undefined

相关文章

  • 兼收并蓄 TypeScript - 基础: boolean
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:boolean示例如下:basic\boolean.ts{leta=true;console.log(a);//true//将指定类型的数据转换为boolean类型console.log(Boolean(100),Boolean(......
  • 易优eyoucms网站报错 \core\library\think\App.php Fatal error: Call to undefin
    当你遇到 Fatalerror:Calltoundefinedfunctionthink\switch_citysite() 这样的错误时,说明在代码中调用了一个未定义的函数 think\switch_citysite()。这种情况通常是因为函数没有被正确地引入或者该函数根本不存在于当前的代码库中。解决方案确认函数的存在检查 s......
  • react react18+vite+typeScript+eslint+prettier+husky+lint-staged+commitlint 快速
    技术栈react18react-router6antd5zustand4vite45axiosfakerjs模拟数据dayjslodashtypescriptechartscommitlint、prettier、eslinthusky、lint-staged自定义commitlint、cz-cli自定义eslint、prettier代码规范技术栈代码格式规范和语法检测vscode:统一前端编辑器。editor......
  • TypeScript入门 (二)控制语句
    引言大家好,我是GISerLiu......
  • ZBLOG PHP提示"Call to undefined function mysql_connect()"错误
    当遇到Z-BlogPHP在PHP7.2上出现 mysql_connect() 未定义的错误时,这是因为PHP7.2默认不再支持MySQL扩展(mysql 扩展)。你需要进行一些调整来使Z-BlogPHP兼容PHP7.2。以下是两种解决方案:解决方案一:降级PHP版本如果你暂时不想修改代码,可以选择降级PHP版本到一......
  • zblogPHP后台在线升级后提示Call to undefined function Redirect_cmd_end()错误
    当Z-BlogPHP在线升级后提示“CalltoundefinedfunctionRedirect_cmd_end()”错误时,这通常是因为升级过程中某些文件没有正确更新或存在兼容性问题。以下是一些可能的解决步骤:1.检查函数定义问题描述:Redirect_cmd_end() 函数可能未被定义。解决方法:打开Z-BlogPHP......
  • Call to undefined function think\exception\config()
    错误信息 Calltoundefinedfunctionthink\exception\config() 表示在ThinkPHP框架中调用了未定义的函数 think\exception\config()。这通常是由于以下几个原因造成的:命名空间问题:可能是命名空间声明不正确或导入了错误的类。配置文件问题:可能是配置文件未正确加载或存在......
  • 范围 for,new 内存动态分配,nullptr
    范围for,new内存动态分配,nullptr范围forintmain(){intarr[]={11,12,13,14,15};for(autox:arr){//问题所在:需要将arr中的每一个都拷贝到x中去===>节省拷贝的方法:使用指针,提升效率cout<<x<<endl;}cout<<"============......
  • TypeScript很麻烦?,不想使用!
    前言最近,我们部门在开发一个组件库时,我注意到一些团队成员对使用TypeScript表示出了抵触情绪,他们常常抱怨说:“TypeScript太麻烦了,我们不想用!”起初,我对此感到困惑:TypeScript真的有那么麻烦吗?然而,当我抽时间审查队伍的代码时,我终于发现了问题所在。在这篇文章中,我想和大家分享我的一......
  • typescript 中的private是什么,你记住了吗?
    在TypeScript中,private关键字用于声明类的成员(属性或方法)为私有的。这意味着这些成员只能在类的内部被访问和修改,而不能在类的外部通过类的实例直接访问。这是面向对象编程(OOP)中封装原则的一个体现,有助于隐藏类的内部实现细节,只暴露必要的接口给外部使用。私有属性私有属性是类的内......