JS的使用

变量的声明和赋值数据类型js的语法结构Js中的函数Js中的数组

变量的声明和赋值

JS中所有类型变量的声明都使用var 1.先声明变量再赋值 var i; i = 5; 2.同时声明和赋值变量 var s= "xyr"; var x, y, z = 10; 3.不声明直接赋值 width=5; 变量可以不经声明而直接使用,但这种方法很容易出错,也很难查找排错,不推荐使用

数据类型

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n6" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">JavaScript 拥有动态类型。相同的变量可用作不同的类型
ECMAScript 有 5 种原始类型(primitive type),即 Undefined、Null、Boolean、Number 和 String。</pre>

undefined Undefined 类型只有一个值,即 undefined。当声明的变量未初始化时,该变量的默认值是 undefined。 var x; 变量 x 没有初始值,将被赋予值undefined null 表示一个空值,与undefined值相等 一种只有一个值的类型是 Null,它只有一个专用值 null,即它的字面量。值 undefined 实际上是从值 null 派生来的,因此 ECMAScript 把它们定义为相等的。 alert(null == undefined); //输出 "true" number 表示一切的数字类型 var num1=23; //整数 var num2=23.0; //浮点数 boolean true和false string 一组被引号(单引号或双引号)括起来的文本 var str="This is a String"; Function:函数类型 Object:表示所有的引用类型(对象类型) javascript中的对象、数组和null

typeof运算符返回值如下 undefined:变量被声明后,但未被赋值 string:用单引号或双引号来声明的字符串 boolean:true或false number:整数或浮点数 object:javascript中的对象、数组和null function: js函数

为什么 typeof 运算符对于 null 值会返回 "Object"。这实际上是 JavaScript 最初实现中的一个错误,然后被 ECMAScript 沿用了。 现在,null 被认为是对象的占位符,从而解释了这一矛盾,但从技术上来说,它仍然是原始值。

typeof() : 获取数据的类型 instancof关键字: 判断左边类型是否是右边类型的实例

如何判断一个类型是否是一个具体的引用类型? typeof() : 获取数据的类型

instancof关键字: 判断左边类型是否是右边类型的实例 如何判断一个类型是否是一个具体的引用类型? 给定一个Date类型,我希望输出Date

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n13" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">typeof只能够判断原始数据类型和Object类型,不能够判断具体的引用类型,比如说Date类型
解决方式: 先采用typeof判断返回的是否是object类型,如果是就是使用instanceof关键字判断是否是Date类型
alert(typeof(date) == "object");
if(typeof(date) == "object"){
// 到了这里说明一定是引用类型
if(date instanceof Date){
alert("Date");
}
}</pre>

选择语句 if语句和Java里面一模一样 单if/if - else/多重if/嵌套if switch语句 Java里面的switch语句可以有哪些类型? byte short int char String(jdk1.7) 枚举 js里面的switch语句可以支持任意类型

循环语句

for、while循环、do-while循环、for-in var fruit=[ "apple", "orange", "peach","banana"]; for(var i in fruit){ document.write(fruit[i]+" "); } break/continue关键字

js的语法结构

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(54, 59, 64); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 30px; border: 1px solid; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1.顺序结构
2.选择结构</pre>

单if if - else 多重if 嵌套if switch语句 Java里面的switch语句可以有哪些类型? byte short int char String(jdk1.7) 枚举 js里面的switch语句可以支持任意类型 3.循环结构 for while do-while

[图片上传失败...(image-f49aa8-1559219005962)]

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,816评论 1 45
  • 第一章: JS简介 从当初简单的语言,变成了现在能够处理复杂计算和交互,拥有闭包、匿名函数, 甚至元编程等...
    LaBaby_阅读 1,706评论 0 6
  • 第一章 前端三大语言:HTML(专门编写网页内容)、CSS(编写网页样式)、JS(专门编写网页交互行为) 能简写尽...
    fastwe阅读 1,057评论 0 0
  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,350评论 0 3
  • 前端07班 王语句JavaScript程序的执行单位为行(line),也就是一行一行地执行。一般情况下,每一行就是...
    ea203453e188阅读 915评论 0 4