当前位置:网站首页>带你详细认识JS基础语法(建议收藏)
带你详细认识JS基础语法(建议收藏)
2022-07-25 15:23:00 【꧁小ۣۖิ鸽ۣۖิ子ۣۖิ꧂】
字面量
概念:一些不可改变的值,可直接使用,但一般不会直接使 用。
例如:1 3 5 7;
变量
概念:用来储存字面量,且值可任意改变。
声明:通过关键字var声明变量。
例如:var btn=5;
标识符
概念:js中所有可以由我们自主命名的都可以成为标识符。
例如:变量名 函数名 属性名都属于标识符
命名规则:1. 可以含有字母、数字、下划线_、$
2.不能以数字开头
3.不能使用ES中的关键字与保留字
4.可以用中文
5.一般采用驼峰写法
例如:小驼峰helloWorld 大驼峰HelloWorld
数据类型
概念:数据类型是字面量的类型
检查数据类型语法:typeof 变量
例:var btn=”123”; console.log(typeof btn);
共有六种数据类型:
基本类型:
1.string字符串
例:var str=”hello world”;
2.number数值
概念:js中所有数值都是number类型,包括整 数、浮点数(小数),如果超过最大值则会返回一个Infinity 表示正无穷大,加个负号则为负无穷
例:var a=123;
3.Null空值
概念:专门用来表示一个空对象
使用typeof检查时则会返回object
4.undifined未定义
概念:声明一个变量但是未给变量赋值时,它的值就是undefined
例:var c;
5.Boolean布尔值 true/false
概念:主要用来做逻辑判断-true为真 -false为假
例:var bl=false;
6.引用类型:
object对象
类型转换
强制类型转换:
概念:将一个数据类型强制转换为其他数据类型
类型转换主要指将其他数据类型转换为
String number Boolean
1.其他数据类型转换为string类型
法一:· 调用toString()方法,该方法不会影响原变量,只会将转换的结果返回
null和undefined这两个值没有toString()方法
法二:调用String()函数,并将被转换的数据作为参数传递给函数
例:
var a=123;//数值类型-》字符串类型
var b=a.toString();
console.log(typeof b);
var bl=false;//布尔类型-》字符串类型
var b=a.toString();
console.log(typeof b);
var c=123;
var d=string;
console.log(typeof c);//number
console.log(typeof d);//string
2.其他数据类型转换为number类型
概念:
法一:
字符串–>数字
1.纯数字的字符串直接转换为数字
2.字符串中有非数字内容则转换为NaN
3.字符串为空串或者全空格则转换为0
布尔值–>数字 true转换为1,false转换为0
null–>数字为0
undefined–>数字为NaN
例:var num=”123”/”123sdf1”/” ”/false/null/undefined;
var a=Number(num);
console.log(a);/123/NaN/0/0/0/NaN
法二:专门用来转换字符串类型
parseInt() 可以将字符串转换为整数,将字符串中有效整数取出来,然后转换为Number
parseFloat() 可以把字符串转换为浮数点(小数),获取字符串中有效的小数
例:var num=”123.23”
num=parseFloat(num);/parseInt(num);
Console.log(num);/123.23/123
3.其他数据类型转换为Boolean类型
方法:使用Boolean()函数
1.数字–>布尔 除了0和NaN是false,其他则全是true
2.字符串–>布尔 除了空串是false,其他则全是true
3.null和undefined 都会转换为false
4.object对象 转换为true
例:var a=123.12;/0/NaN
a=Boolean(a);
console.log(a);/true/false/false
var str=”234fg”;/””/undefined
s=Boolean(str);
console.log(s);/true/false/false
边栏推荐
- The difference between Apple buy in and apple pay
- 海缆探测仪TSS350(一)
- Run redis on docker to start in the form of configuration file, and the connection client reports an error: server closed the connection
- Rediscluster setup and capacity expansion
- MySQL transactions and mvcc
- JS 同步、异步,宏任务、微任务概述
- 如何更新更新数据库中的json值?
- ML - 自然语言处理 - 基础知识
- Sublimetext-win10 cursor following problem
- Hbck 修复问题
猜你喜欢

MySQL transactions and mvcc

How to solve the problem of scanf compilation error in Visual Studio

推荐10个堪称神器的学习网站

What is the Internet of things

outline和box-shadow实现外轮廓圆角高光效果

matlab--CVX优化工具包安装

Spark partition operators partitionby, coalesce, repartition

为什么PrepareStatement性能更好更安全?

Spark submission parameters -- use of files

ML - natural language processing - Basics
随机推荐
How to solve the login problem after the 30 day experience period of visual stuido2019
在win10系统下使用命令查看WiFi连接密码
为什么PrepareStatement性能更好更安全?
ML - Speech - advanced speech model
从 join on 和 where 执行顺序认识T-sql查询执行顺序
二进制补码
小波变换--dwt2 与wavedec2
MATLAB读取显示图像时数据格式转换原因
基于OpenCV和YOLOv3的目标检测实例应用
ML - 自然语言处理 - 关键技术
JS 同步、异步,宏任务、微任务概述
UITextField的inputView和inputAccessoryView注意点
Args parameter parsing
mysql heap表_MySQL内存表heap使用总结-九五小庞
JVM知识脑图分享
Application of C language array in Sanzi chess -- prototype of Queen n problem
The development summary of the function of fast playback of audio and video in any format on the web page.
Spark DF adds a column
Notes on inputview and inputaccessoryview of uitextfield
谷歌云盘如何关联Google Colab