型別、比較與數字

== vs ===、型別轉換、null/undefined 與數字格式。

22 / 22 題 · 依票數排序

4.3k

How to round to at most 2 decimal places, if necessary

I'd like to round at most two decimal places, but only if necessary . Input: 10 1.7777777 9.1 Output: 10 1.78 9.1 How can I do this in JavaScript?

2012javascriptroundingdecimal-point·3 重點答案·5M 次瀏覽
3.6k

How can I convert a string to boolean in JavaScript?

Can I convert a string representing a boolean value (e.g., 'true', 'false') into an intrinsic type in JavaScript? I have a hidden form in HTML that is updated based on a user's se…

2008javascriptboolean-expressionboolean-operationsstring-conversion·3 重點答案·3.7M 次瀏覽
3.1k

How can I check for "undefined" in JavaScript?

What is the most appropriate way to test if a variable is undefined in JavaScript? I've seen several possible ways: if (window.myVariable) Or if (typeof(myVariable) != "undefined"…

2010javascriptundefined·3 重點答案·3.8M 次瀏覽
2.8k

How can I format a number with commas as thousands separators?

I am trying to print an integer in JavaScript with commas as thousands separators. For example, I want to show the number 1234567 as "1,234,567". How would I go about doing this?…

2010javascriptformattingnumbers·3 重點答案·2.3M 次瀏覽
2.6k

Generate random number between two numbers in JavaScript

Is there a way to generate a random number in a specified range with JavaScript ? For example : a specified range from 1 to 6 were the random number could be either 1, 2, 3, 4, 5,…

2011javascriptrandom·3 重點答案·2.7M 次瀏覽
2.6k

Validate decimal numbers in JavaScript - IsNumeric()

What's the cleanest, most effective way to validate decimal numbers in JavaScript? Bonus points for: Clarity. Solution should be clean and simple. Cross-platform. Test cases: 01.…

2008javascriptvalidationnumbers·3 重點答案·1.6M 次瀏覽
2.5k

How to format numbers as currency strings?

I would like to format a price in JavaScript. I'd like a function which takes a float as an argument and returns a string formatted like this: $ 2,500.00 How can I do this?

2008javascriptformattingcurrencynumber-formatting·3 重點答案·3M 次瀏覽
2.4k

What is the preferred syntax for defining enums in JavaScript?

What is the preferred syntax for defining enums in JavaScript? Something like: my.namespace.ColorEnum = { RED : 0, GREEN : 1, BLUE : 2 } // later on if(currentColor == my.namespac…

2008javascriptsyntaxenums·3 重點答案·1.3M 次瀏覽
1.9k

Why does ++[[]][+[]]+[+[]] return the string "10"?

This is valid and returns the string "10" in JavaScript ( more examples here ): console.log(++[[]][+[]]+[+[]]) Why? What is happening here?

2011javascriptsyntaxjsfuck·3 重點答案·238k 次瀏覽
1.7k

Difference between == and === in JavaScript

What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?

2009javascriptcomparison-operatorsequality-operatoridentity-operator·2 重點答案·1.1M 次瀏覽
1.6k

Is there a "null coalescing" operator in JavaScript?

Is there a null coalescing operator in Javascript? For example, in C#, I can do this: String someString = null; var whatIWant = someString ?? "Cookies!"; The best approximation I…

2009javascriptoperatorsnull-coalescing-operatornull-coalescing·3 重點答案·487k 次瀏覽