search
HomeWeb Front-endJS TutorialJS uses switch to determine the code sharing of ternary operation while and attribute operation

This article mainly introduces JS switch to judge the ternary operation while and attribute operation code. Friends who need it can refer to

Ternary operation: such as var a = 10;


var b= 12;
c = a>b ?a:b;

If true, execute a, otherwise execute b


var isHide = true;

If you use if to judge the statement as follows


if(isHide) {
box.style.display = "block";
} else {
box.style.display = "none";
}

Ternary operation replaces if


box.style.display = isHide?"block":"none";

Judgment condition? The execution statement is established: the execution statement is not established.

If the condition is true, return: the result of the execution is true

The condition is not true: the result of the execution

Simplified version of if judgment

continue Skip this loop: cannot write ternary operation

column:


for(var i=0;i<5;i++){
if(i==3)
continue;跳过i=3这个循环不执行;其他依然执行。
}

break: terminate the entire loop; by default, only the immediately adjacent loop will be terminated If you want to terminate other loops, you need to give the loop a name.

Example:


for(var i=0;i<5;i++){
if(i==3)
break;当循环执行到i=3时终止整个循环。
}
name:for(var i=0;i<5;i++){
for(var j=0;j<5;j++){
break name //终止两个循环
}
}

The object stores data, which is stored in the properties of the object:

The object is a complex type of data , we can store any type of data in the object

The first type: attribute operation 1 The first type of attribute operation is a set of hard-coded values, and variables cannot be used.

Read operation obj.Attribute name

Write operation obj.Attribute name = attribute value;

obj.key = val;Key value pair;

For example,


var obj = {};
obj.name = "Leo"; 这种写法为第一种属性名都被写死若想调用必须要如
obj.age = 40; 下console.log(obj.name);。
obj.gender = "female";
console.log(obj.w);//注意第一种属性操作是写死的一个值,不能使用变量 obj.w 这么写就认为在找 obj的w属性。

The second attribute operation is done. What is received is a string. The string can be stored in a variable. You can directly write obj[variable Call]

Read operation obj["Attribute name]

obj["Attribute name"]=Attribute value;


obj["key"] = val;

Note The second type

such as


var obj = {};
obj[&#39;name&#39;] = "Leo";
obj[&#39;age&#39;] = 40;
obj[&#39;gender&#39;] = "female";
obj[&#39;width&#39;] = "180px";
obj[&#39;height&#39;] = "180px";
obj[&#39;children&#39;] = ["大明","小明","三明"];
var w = "width";
console.log(obj[w]);// 第二种属性操作,接受的是一个字符串,字符串就可以存在变量中,可以直接写obj[变量调用];
alert("name" in obj); // obj 有 name属性就返回 true,否则 返回false

determines whether there is this attribute in the object

returns true or false


for (var s in obj) { 找到obj的所有属性 s会依次变成arr的属性名 //同过in方法将循环变量s变成obj对
console.log(s,obj[s]); 应的属性名。若想使用只有用第二中命名。s的类型是字符串。
} s为属性名 obj[S]为对应的属性名的属性值。

for in can be used to loop objects and arrays but cannot be used to loop a group of elements.

for loop can only be used to loop arrays (a group of elements) and cannot be used Loop through a group of objects


##

###while语句###
起始值
while(判断条件){
执行语句;
自增;
}
##switch##
switch(要判断的内容){
case:判断内容; //条件成立后执行语句将case下所有代码都执行,只有遇见break才会结束
执行语句; 若没有遇见break就会遇见穿刺现象执行所有代码。
break;
default: //以上所有的判断都不成立
}

Example

##

switch(a){
case 10://当a == 10;
console.logo(“;;;;”)语句结束之后添加break
break;//当a==10 的
case 15://当a==15;
console.logo("ppppp")
break;
default: //以上所有的判断都不成立
}

The above is the detailed content of JS uses switch to determine the code sharing of ternary operation while and attribute operation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),