JavaScript Operators - W3Schools
文章推薦指數: 80 %
Assignment operators are fully described in the JS Assignment chapter. JavaScript String Operators. The + operator can also be used to add (concatenate) strings ... Tutorials References Exercises Videos Menu Login Website GetCertified ProNEW HTML CSS JAVASCRIPT SQL PYTHON PHP BOOTSTRAP HOWTO W3.CSS JAVA JQUERY C C++ C# R React Darkmode Darkcode × Tutorials HTMLandCSS LearnHTML LearnCSS LearnRWD LearnBootstrap LearnW3.CSS LearnColors LearnIcons LearnGraphics LearnSVG LearnCanvas LearnHowTo LearnSass DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery JavaScript LearnJavaScript LearnjQuery LearnReact LearnAngularJS LearnJSON LearnAJAX LearnAppML LearnW3.JS Programming LearnPython LearnJava LearnC LearnC++ LearnC# LearnR LearnKotlin LearnGo LearnDjango LearnTypeScript ServerSide LearnSQL LearnMySQL LearnPHP LearnASP LearnNode.js LearnRaspberryPi LearnGit LearnAWSCloud WebBuilding CreateaWebsiteNEW WhereToStart WebTemplates WebStatistics WebCertificates WebDevelopment CodeEditor TestYourTypingSpeed PlayaCodeGame CyberSecurity Accessibility DataAnalytics LearnAI LearnMachineLearning LearnDataScience LearnNumPy LearnPandas LearnSciPy LearnMatplotlib LearnStatistics LearnExcel LearnGoogleSheets XMLTutorials LearnXML LearnXMLAJAX LearnXMLDOM LearnXMLDTD LearnXMLSchema LearnXSLT LearnXPath LearnXQuery × References HTML HTMLTagReference HTMLBrowserSupport HTMLEventReference HTMLColorReference HTMLAttributeReference HTMLCanvasReference HTMLSVGReference GoogleMapsReference CSS CSSReference CSSBrowserSupport CSSSelectorReference Bootstrap3Reference Bootstrap4Reference W3.CSSReference IconReference SassReference JavaScript JavaScriptReference HTMLDOMReference jQueryReference AngularJSReference AppMLReference W3.JSReference Programming PythonReference JavaReference ServerSide SQLReference MySQLReference PHPReference ASPReference XML XMLDOMReference XMLHttpReference XSLTReference XMLSchemaReference CharacterSets HTMLCharacterSets HTMLASCII HTMLANSI HTMLWindows-1252 HTMLISO-8859-1 HTMLSymbols HTMLUTF-8 × ExercisesandQuizzes Exercises HTMLExercises CSSExercises JavaScriptExercises SQLExercises MySQLExercises PHPExercises PythonExercises NumPyExercises PandasExercises SciPyExercises jQueryExercises JavaExercises C++Exercises C#Exercises RExercises KotlinExercises GoExercises BootstrapExercises Bootstrap4Exercises Bootstrap5Exercises GitExercises Quizzes HTMLQuiz CSSQuiz JavaScriptQuiz SQLQuiz MySQLQuiz PHPQuiz PythonQuiz NumPyQuiz PandasQuiz SciPyQuiz jQueryQuiz JavaQuiz C++Quiz C#Quiz RQuiz KotlinQuiz XMLQuiz BootstrapQuiz Bootstrap4Quiz Bootstrap5Quiz CyberSecurityQuiz AccessibilityQuiz Courses HTMLCourse CSSCourse JavaScriptCourse FrontEndCourse SQLCourse PHPCourse PythonCourse NumPyCourse PandasCourse DataAnalyticsCourse jQueryCourse JavaCourse C++Course C#Course RCourse XMLCourse CyberSecurityCourse AccessibilityCourse Certificates HTMLCertificate CSSCertificate JavaScriptCertificate FrontEndCertificate SQLCertificate PHPCertificate PythonCertificate DataScienceCertificate Bootstrap3Certificate Bootstrap4Certificate jQueryCertificate JavaCertificate C++Certificate ReactCertificate XMLCertificate × Tutorials References Exercises PaidCourses Spaces Videos Shop Pro JSTutorial JSHOME JSIntroduction JSWhereTo JSOutput JSStatements JSSyntax JSComments JSVariables JSLet JSConst JSOperators JSArithmetic JSAssignment JSDataTypes JSFunctions JSObjects JSEvents JSStrings JSStringMethods JSStringSearch JSStringTemplates JSNumbers JSNumberMethods JSArrays JSArrayMethods JSArraySort JSArrayIteration JSArrayConst JSDates JSDateFormats JSDateGetMethods JSDateSetMethods JSMath JSRandom JSBooleans JSComparisons JSIfElse JSSwitch JSLoopFor JSLoopForIn JSLoopForOf JSLoopWhile JSBreak JSIterables JSSets JSMaps JSTypeof JSTypeConversion JSBitwise JSRegExp JSErrors JSScope JSHoisting JSStrictMode JSthisKeyword JSArrowFunction JSClasses JSModules JSJSON JSDebugging JSStyleGuide JSBestPractices JSMistakes JSPerformance JSReservedWords JSVersions JSVersions JS2009(ES5) JS2015(ES6) JS2016 JS2017 JS2018 JSIE/Edge JSHistory JSObjects ObjectDefinitions ObjectProperties ObjectMethods ObjectDisplay ObjectAccessors ObjectConstructors ObjectPrototypes ObjectIterables ObjectSets ObjectMaps ObjectReference JSFunctions FunctionDefinitions FunctionParameters FunctionInvocation FunctionCall FunctionApply FunctionBind FunctionClosures JSClasses ClassIntro ClassInheritance ClassStatic JSAsync JSCallbacks JSAsynchronous JSPromises JSAsync/Await JSHTMLDOM DOMIntro DOMMethods DOMDocument DOMElements DOMHTML DOMForms DOMCSS DOMAnimations DOMEvents DOMEventListener DOMNavigation DOMNodes DOMCollections DOMNodeLists JSBrowserBOM JSWindow JSScreen JSLocation JSHistory JSNavigator JSPopupAlert JSTiming JSCookies JSWebAPIs WebAPIIntro WebFormsAPI WebHistoryAPI WebStorageAPI WebWorkerAPI WebFetchAPI WebGeolocationAPI JSAJAX AJAXIntro AJAXXMLHttp AJAXRequest AJAXResponse AJAXXMLFile AJAXPHP AJAXASP AJAXDatabase AJAXApplications AJAXExamples JSJSON JSONIntro JSONSyntax JSONvsXML JSONDataTypes JSONParse JSONStringify JSONObjects JSONArrays JSONServer JSONPHP JSONHTML JSONJSONP JSvsjQuery jQuerySelectors jQueryHTML jQueryCSS jQueryDOM JSGraphics JSGraphics JSCanvas JSPlotly JSChart.js JSGoogleChart JSD3.js JSExamples JSExamples JSHTMLDOM JSHTMLInput JSHTMLObjects JSHTMLEvents JSBrowser JSEditor JSExercises JSQuiz JSCertificate JSReferences JavaScriptObjects HTMLDOMObjects JavaScriptOperators ❮Previous Next❯ Example Assignvaluestovariablesandaddthemtogether: letx=5; //assignthevalue5to x lety=2; //assignthevalue 2toy letz=x+y; //assignthevalue7toz(5+2) TryitYourself» Theassignmentoperator(=)assignsavaluetoavariable. Assignment letx=10; TryitYourself» Theadditionoperator(+)addsnumbers: Adding letx=5; lety=2; letz=x+y; TryitYourself» Themultiplicationoperator(*)multipliesnumbers. Multiplying letx=5; lety=2; letz=x*y; TryitYourself» JavaScriptArithmeticOperators Arithmeticoperatorsareusedtoperformarithmeticonnumbers: Operator Description + Addition - Subtraction * Multiplication ** Exponentiation(ES2016) / Division % Modulus(DivisionRemainder) ++ Increment -- Decrement Arithmeticoperatorsarefullydescribedinthe JSArithmeticchapter. JavaScriptAssignmentOperators AssignmentoperatorsassignvaluestoJavaScriptvariables. Operator Example SameAs = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y %= x%=y x=x%y **= x**=y x=x**y Theadditionassignmentoperator(+=)addsavaluetoavariable. Assignment letx=10;x+=5; TryitYourself» Assignmentoperatorsarefullydescribedinthe JSAssignmentchapter. JavaScriptStringOperators The+operatorcanalsobeusedtoadd(concatenate)strings. Example lettext1="John"; lettext2="Doe"; lettext3=text1+""+text2; Theresultoftext3willbe: JohnDoe TryitYourself» The+=assignmentoperatorcanalsobeusedtoadd(concatenate)strings: Example lettext1="Whatavery"; text1+="niceday"; Theresultoftext1willbe: Whataveryniceday TryitYourself» Whenusedonstrings,the+operatoriscalledtheconcatenationoperator. AddingStringsandNumbers Addingtwonumbers,willreturnthesum,butaddinganumberandastringwillreturnastring: Example letx=5+5; lety="5"+5; letz="Hello"+5; Theresultofx,y,andzwillbe: 10 55 Hello5 TryitYourself» Ifyouaddanumberandastring,theresultwillbeastring! JavaScriptComparisonOperators Operator Description == equalto === equalvalueandequaltype != notequal !== notequalvalueornotequaltype > greaterthan < lessthan >= greaterthanorequalto <= lessthanorequalto ? ternaryoperator Comparisonoperatorsarefullydescribedinthe JSComparisonschapter. JavaScriptLogicalOperators Operator Description && logicaland || logicalor ! logicalnot Logicaloperatorsarefullydescribedinthe JSComparisonschapter. JavaScriptTypeOperators Operator Description typeof Returnsthetypeofavariable instanceof Returnstrueifanobjectisaninstanceofanobjecttype TypeoperatorsarefullydescribedintheJSTypeConversionchapter. JavaScriptBitwiseOperators Bitoperatorsworkon32bitsnumbers. Anynumericoperandintheoperationisconvertedintoa32bitnumber. TheresultisconvertedbacktoaJavaScriptnumber. Operator Description Example Sameas Result Decimal & AND 5&1 0101&0001 0001 1 | OR 5|1 0101|0001 0101 5 ~ NOT ~5 ~0101 1010 10 ^ XOR 5^1 0101^0001 0100 4 << leftshift 5<<1 0101<<1 1010 10 >> rightshift 5>>1 0101>>1 0010 2 >>> unsignedrightshift 5>>>1 0101>>>1 0010 2 Theexamplesaboveuses4bitsunsignedexamples.ButJavaScriptuses32-bitsignednumbers. Becauseofthis,inJavaScript,~5willnotreturn10.Itwillreturn-6. ~00000000000000000000000000000101willreturn11111111111111111111111111111010 BitwiseoperatorsarefullydescribedintheJS Bitwisechapter. TestYourselfWithExercises Exercise: Multiply10with5,andalerttheresult. alert(105); SubmitAnswer» StarttheExercise ❮Previous Next❯ NEW WejustlaunchedW3Schoolsvideos Explorenow COLORPICKER Getcertifiedbycompletingacoursetoday! w3schoolsCERTIFIED.2022 Getstarted CODEGAME PlayGame
延伸文章資訊
- 1運算式與運算子- JavaScript
在JavaScript 中基本的關鍵字與一般的運算式。 this: this 關鍵字可以參考執行函數的所在位置。 function (en-US): function 關鍵字可以定義一個函數 ...
- 2JavaScript and JSON Essentials - Google 圖書結果
JavaScript,whichwasintroduced asLiveScriptbyNetscape CommunicationsCorp,has grown leapsandboundsi...
- 3JavaScript 基礎知識-邏輯運算子&&,|| - iT 邦幫忙
JavaScript 基礎知識-邏輯運算子&&,|| ... 生活上也會有類似的邏輯判斷,例如:媽媽拜託小明去市場買菜,要買到牛肉,青菜和洋蔥,成功買到這三樣,媽媽才 ...
- 4JavaScript 邏輯運算&& ( and )、| | ( or )、!( not )
- 5Logical AND (&&) - JavaScript - MDN Web Docs