当前位置:网站首页>JS第二天(变量、变量的使用、命名规则、语法扩展)
JS第二天(变量、变量的使用、命名规则、语法扩展)
2022-07-27 04:09:00 【枕頭說他不想醒】
一、什么是变量
就是一个装东西的盒子,变量是用于存放的数据容器,指代计算机中的一段内存空间,我们通过变量名获取数据,甚至数据可以修改
本质:变量是程序在内存申请的一块用来存放数据的空间
二、变量的使用
变量在使用时分为两步:
1、声明变量 开辟内存空间
var age;//声明变量:2、赋值 向内存空间中储存数据
age = 18;
3、输出结果
console.log(age);4、变量的初始化:声明一个变量并赋值,我们称之为变量的初始化
//1、声明一个age的变量
var age;
//2、赋值,把值存入变量中
age = 18;
//3、输出结果
// console.log(age);
//4、变量的初始化
var myname = '枕头睡不醒';
console.log(myname);
//声明变量的同时赋值为18
//声明一个变量并赋值,我们称之为变量的初始化三、变量的命名规则
1、变量名可以由 数字 字母 下划线 元素组成
2、不能以数字开头
3、不能使用关键字和保留字 var for if class
4、要见名之意
5、驼峰命名:大驼峰 每个单词的首字母都要大写 Student Name
小驼峰 第一个单词的首字母小写,其余的单词首字母单鞋 student Name
四、变量的语法扩展
1、更新变量: 一个变量被重新赋值后,他原有的值就会被覆盖,变量值将以最后一次赋的值为准
代码展示:
<script>
//1、更新变量
var myname = '老师';
console.log(myname);
myname = '枕头睡不醒';
console.log(myname);
</script>结果展示:

2、声明多个变量时,只需要写一个var,多个变量名之间使用英文逗号隔开
//声明多个变量时,只需要写一个var,多个变量名之间使用英文逗号隔开
// var age = 18
// var address = '火影';
// var gz = 2000;
var age = 18,
address = '火影'
gz = '2000'3、声明变量的特殊情况
(1)只声明,不赋值,结果是undefined 未定义的
代码展示:
var sex;
console.log(sex);图片展示:
![]()
(2)不声明也不赋值,直接使用某个变量一定会报错的
代码展示:
console.log(tel);图片展示:
(3)不声明直接赋值使用,能得到结果,但不推荐使用
代码展示:
qq = 110;
console.log(qq)图片展示:

最好的写法:声明变量并赋值
边栏推荐
- Convolution neural network -- convolution of gray image
- Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
- [small sample segmentation] msanet: multi similarity and attention guidance for boosting few shot segmentation
- 详解左值、右值、左值引用以及右值引用
- EVT interface definition file of spicy
- RSA asymmetric encryption and decryption signature verification tool
- ELS square display principle
- The new Internet era has come. What new opportunities will Web 3.0 bring us
- 标准C语言11
- 你了解微信商户分账吗?
猜你喜欢
![[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)](/img/6a/02c76f5bd35b2d89e4f471b9b688f5.png)
[machine learning network] BP neural network and deep learning-6 deep neural networks (DNN)

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
![[competition reference] pytorch common code snippet and operation collection](/img/b3/0b91e381e2444dfd222537bf5b8ccf.jpg)
[competition reference] pytorch common code snippet and operation collection

Remember the major performance problems caused by a TCP packet loss

Spark practice case (upgraded version)

卷积神经网络——24位彩色图像的卷积的详细介绍
![Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量

【day02】数据类型转换、运算符、方法入门

The difference between ArrayList and LinkedList

深度剖析 —— 动态内存管理
随机推荐
els_ 画矩形、代码规划和备份
5.component动态组件的展示
BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
Practice of microservice in solving Library Download business problems
Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
Word/excel has a fixed table size. When filling in the content, the table does not change with the cell content
The difference between ArrayList and LinkedList
P1438 无聊的数列 线段树+差分
Is the e-commerce billing system important? How should the platform choose billing service providers?
Brightcove任命Dan Freund为首席营收官
Wechat input component adds a clear icon, and clicking clear does not take effect
Standard C language 11
Some common instructions in JVM tuning
STM32 serial port based on Hal library accepts interrupts and idle interrupts
Plato Farm全新玩法,套利ePLATO稳获超高收益
无有线网络下安装并配置debian
Use the kubesphere graphical interface dashboard to enable the Devops function
Final review of management information system
从零开始C语言精讲篇4:数组
RSA 非对称 加密解密 加签验签工具