JavaScript에서 산술 연산자는 숫자(리터럴 또는 변수)에 대한 산술 연산을 수행하는 데 사용되는 기호입니다. 여기에는 더하기 연산자 "+", 빼기 연산자 "-", 곱하기 연산자 "*", 나누기 연산자 "/", 나머지가 포함됩니다. 연산자 "%", 숫자 부정 연산자 "-".
이 튜토리얼의 운영 환경: Windows 7 시스템, JavaScript 버전 1.8.5, Dell G3 컴퓨터.
JavaScript에서 산술 연산자는 숫자(리터럴 또는 변수)에 대한 산술 연산을 수행하는 데 사용되는 기호입니다. 기본 산술 연산(산술 연산자)을 완료하는 데 사용되는 기호이며 네 가지 산술 연산을 처리하는 데 사용됩니다.
산술 연산자에는 더하기 +, 빼기 -, 곱하기 *, 나누기 /, 나머지 연산자 % 및 숫자 부정 연산자 -가 포함됩니다.
연산자 | 설명 | 예 |
---|---|---|
+ | 연산자 | x + y를 추가하면 x와 y의 합을 계산하는 것을 의미합니다. |
* | 곱셈 연산자 | |
/ | 나눗셈 연산자 | |
% | 모듈러스(나머지) 연산자 | |
더하기 연산자 |
예 1 지불 합산 연산에 주의 특수 피연산자.
var n = 5; //定义并初始化任意一个数值 console.log(NaN + n); //NaN与任意操作数相加,结果都是NaN console.log(Infinity + n); //Infinity与任意操作数相加,结果都是Infinity console.log(Infinity + Infinity); //Infinity与Infinity相加,结果是Infinity console.log((-Infinity) + (-Infinity)); //负Infinity相加,结果是负Infinity console.log((-Infinity) + Infinity); //正负Infinity相加,结果是NaN
예제 2
더하기 연산자는 피연산자의 데이터 유형에 따라 더하기 또는 연결할지 여부를 결정할 수 있습니다.
console.log(1 + 1); //如果操作数都是数值,则进行相加运算 console.log(1 + "1"); //如果操作数中有一个是字符串,则进行相连运算 console.log(3.0 + 4.3 + ""); //先求和,再连接,返回"7.3" console.log(3.0 + "" + 4.3); //先连接,再连接,返回"34.3" //3.0转换为字符串3
더하기 연산자를 사용할 때는 먼저 피연산자의 데이터 유형이 요구 사항을 충족하는지 확인해야 합니다.
뺄셈 연산자
예제 1특수 피연산자의 뺄셈 연산에 주의하세요.
var n = 5; //定义并初始化任意一个数值 console.log(NaN - n); //NaN与任意操作数相减,结果都是NaN console.log(Infinity - n); //Infinity与任意操作数相减,结果都是Infinity console.log(Infinity - Infinity); //Infinity与Infinity相减,结果是NaN console.log((-Infinity) - (-Infinity)); //负Infinity相减,结果是NaN console.log((-Infinity) - Infinity); //正负Infinity相减,结果是-Infinity
예제 2
뺄셈 연산에서 피연산자가 문자열이면 숫자 값으로 변환한 후 연산을 수행해 보세요. 피연산자 중 하나가 숫자가 아니면 NaN이 반환됩니다.
console.log(2 - "1"); //返回1 console.log(2 - "a"); //返回NaN
값을 숫자로 빠르게 변환하려면 마이너스 0 값을 사용하세요. 예를 들어 HTTP 요청의 쿼리 문자열은 일반적으로 문자열 숫자입니다. 먼저 이러한 매개변수 값에서 0을 빼서 숫자 값으로 변환할 수 있습니다. 이것은 parFloat() 메서드를 호출하는 것과 동일한 결과를 가지지만 빼기가 더 효율적이고 빠릅니다. 빼기 연산자를 사용한 암시적 변환은 실패할 경우 NaN을 반환합니다. 이는 parsFloat() 메서드를 사용하여 변환을 수행할 때의 반환 값과 다릅니다. 예를 들어 문자열 "100aaa"의 경우,parseFloat() 메서드는 처음 몇 개의 숫자를 구문 분석할 수 있지만 빼기 연산자의 경우 변환하려면 완전한 숫자여야 합니다.
console.log(parseFloat("100aaa")); //返回100 console.log("100aaa" - 0); //返回NaN
부울 값의 경우, parseFloat() 메서드는 true를 1로, false를 0으로 변환할 수 있으며, 빼기 연산자는 이를 NaN으로 처리합니다.
객체의 경우, parseFloat() 메서드는 변환을 위해 객체의 toString() 메서드를 호출하려고 시도하는 반면, 빼기 연산자는 먼저 변환을 위해 객체의 valueOf() 메서드를 호출하려고 시도한 다음, 실패 후 변환을 위해 toString()을 호출합니다. .
부정 연산부정 연산자는 단항 연산자로, 단항 빼기 연산자라고도 합니다.
Example특수 피연산자의 부정 연산에 주의하세요.
console.log(- 5); //返回-5。正常数值取负数 console.log(- "5"); //返回-5。先转换字符串数字为数值类型 console.log(- "a"); //返回NaN。无法完全匹配运算,返回NaN console.log(- Infinity); //返回-Infinity console.log(- (- Infinity)); //返回Infinity console.log(- NaN); //返回NaN
단항 뺄셈 연산자에 해당하는 것은 단항 덧셈 연산자로, 값을 숫자 값으로 빠르게 변환하는 데 사용할 수 있습니다.
곱셈 연산자
특수 피연산자의 곱셈 연산에 주의하세요. var n = 5; //定义并初始化任意一个数值
console.log(NaN * n); //NaN与任意操作数相乘,结果都是NaN
console.log(Infinity * n); //Infinity与任意非零正数相乘,结果都是Infinity
console.log(Infinity * (- n)); //Infinity与任意非零负数相乘,结果是-Infinity
console.log(Infinity * 0); //Infinity与0相乘,结果是NaN
console.log(Infinity * Infinity); //Infinity与Infinity相乘,结果是Infinity
나눗셈 연산자
특수 피연산자의 나눗셈 연산에 주의하세요. var n = 5; //定义并初始化任意一个数值
console.log(NaN / n); //如果一个操作数是NaN,结果都是NaN
console.log(Infinity / n); //Infinity被任意数字除,结果是Infinity或-Infinity
//符号由第二个操作数的符号决定
console.log(Infinity / Infinity); //返回NaN
console.log(n / 0); //0除一个非无穷大的数字,结果是Infinity或-Infinity,符号由第二个操作数的符号决定
console.log(n / -0); //返回-Infinity,解释同上
나머지 연산자
나머지 연산은 모듈러 연산이라고도 합니다. 예: console.log(3 % 2); //返回余数1
모듈러 연산은 주로 정수에서 작동하지만 부동 소수점 숫자에도 적용됩니다. 예: console.log(3.1 % 2.3); //返回余数0.8000000000000003
Example
특수 피연산자의 나머지 연산에 주의하세요.
var n = 5; //定义并初始化任意一个数值 console.log(Infinity % n); //返回NaN console.log(Infinity % Infinity); //返回NaN console.log(n % Infinity); //返回5 console.log(0 % n); //返回0 console.log(0 % Infinity); //返回0 console.log(n % 0); //返回NaN console.log(Infinity % 0); //返回NaN
【관련 추천:
javascript 학습 튜토리얼】위 내용은 자바스크립트 산술 연산자란 무엇인가요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

usestate () isareacthookusedtomanagestatefunctionalcomponents.1) itinitializesandupdatesstate, 2) workaledtthetThetThepleFcomponents, 3) canleadto'Stalestate'ifnotusedCorrecrally 및 4) performancanoptimizedUsecandusecaldates.

Reactispopularduetoitscomponent 기반 아카데입, 가상, Richcosystem 및 declarativenature.1) 구성 요소 기반 ectureallowsforeusableuipieces, Modularityandmainability 개선 가능성.

TodebugreactApplicationseffective, UsetheseStradegies : 1) 주소 propdrillingwithContapiorredux.2) handleaSnchronousOperationswithUsestAndUseefect, abortControllerTopReceConditions.3) 최적화 formanceSeMoAnduseCalbackTooid

usestate () inreactAllowsStateManagementInfunctionalComponents.1) itsimplifiessTatemanagement, 2) usethepRevCountFunctionToupDatesTestateSpreviousValue, PropeingStaleScallanceBackferperperperperperperperperperperperperpertoptiMizatio

chelectionSimple, IndependentStateVaribles; useUserEducer () useuserEducer () forcomplexStateLogicor () whenStatedSonpreviousState.1) usestate () isidealforsimpleupdatesliketogglingabooleorupdatingacounter.2) usbetterformanagingmentiplesub-vvalusorac

Usestate는 클래스 구성 요소 및 기타 상태 관리 솔루션보다 우수합니다. 국가 관리를 단순화하고 코드를 더 명확하게하고 읽기 쉽고 React의 선언적 특성과 일치하기 때문입니다. 1) Usestate는 함수 구성 요소에서 상태 변수를 직접 선포 할 수있게합니다. 2) 후크 메커니즘을 통해 다시 렌더링하는 동안 상태를 기억합니다.

useUsestate () forlocalcomponentStateManagement; 고려 사항 forglobalstate, complexlogic, orperformanceissues.1) usestate () isidealforsimple, localstate.2) useglobalstatesolutionslikereduxorcontextforsharedstate.3) optforredooxtoolkitormobxcomcoccomcoccomcoccomcoccomcoccomcoccomcoccomcoccomporccomcoccomporccomcoccomport

reusablecomponentsinreacececodemainabenabilityandefficiency는 hallowingesamecomponentacrossdifferentpartsofanapplicationorprojects.1) 그들을 retuduceredundancyandsimplifyupdates.2) theyseconsistencyinuserexperience.3) theyquireoptim


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구
