JavaScript基础知识2
1、运算符
算数(注意+:只要符号任意一边是字符串,就会进行字符拼接)、
赋值、比较(结果为布尔值;==只比较值,而===值和数据类型都要相等)
逻辑(与&&、或||、非!) 自增自减++、--
2、语句
(1)条件分支语句if
是否是闰年
<script>
var year=2100
if(year%4===0 && year%100!==0 || year%400===0){
console.log(year+'是闰年!')
}else{
console.log(year+'不是闰年!')
}
</script>
(2)条件分支语句switch
一年中的第几天
<script>
var year = 2000
var month = 8
var date = 8
var total = 0
switch(month){
case 12: total += 30;
case 11: total += 31;
case 10: total += 30;
case 9: total += 31;
case 8: total += 31;
case 7: total += 30;
case 6: total += 31;
case 5: total += 30;
case 4: total += 31;
case 3:
if(year%4===0 && year%100!==0 || year%400===0){
total += 29
}else{
total += 28
}
case 2: total += 31;
}
total += date
console.log(total)
</script>
(3)循环结构语句while
10的阶乘
<script>
var n=10
var total=1
while(n>=1){
total*=n
n--
}
console.log(total)
</script>
(4)循环结构语句dowhile
爱你
<script>
do {
var result = prompt('你爱不爱我?')
}while(result!=='爱')
alert('我也爱你~')
</script>
(5)循环结构语句for
输出1-100间所有3的倍数
<script>
for(var i=1;i<=100;i++){
if(i%3===0){
console.log(i)
}
}
</script>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 【.NET】调用本地 Deepseek 模型