JavaScript Is “eval” the only way to use JS “switch” as an expression?

You can use a direct approach with an array and calculate the index when you have values with fixed interval. So according to your values, you need

value index comment
----- ----- ------------------------------------------
 0.0     0  take integer of value * 10 
 0.05    0  see above
 0.1     1  see above
 0.15    1  see above
 0.2     2  see above
 0.25    2  see above
 0.3     3  see above
 0.35    3  covert by condition and a fixed value of 3
 0.40    3  see above

This answer may not look good for four values, but when it comes to more values with fixed intervals, than it is easier to think about an other structure of decision logic.

function color(x) {
    return ['#f0f0f0', '#d3eadf', '#bce1cd', '#9cd4b9'][x > 0.3 ? 3 : x * 10 | 0];
}
var i;
for (i = 0; i < 0.5; i += 0.05) {
    document.write(i.toFixed(4) + ' ' + color(i) + '<br>');
}
Answer:1

An alternative is to use a function and invoke it immediately:

var aVariable = (function(){  switch(true){
  case x<0.1: return '#f0f0f0';
  case x<0.2: return '#d3eadf';
  case x<0.3: return '#bce1cd';
  case x>=0.3: return '#9cd4b9';
}})(); // call it immediately

You can also do this trick with ifs since return short circuits:

var aVariable = (function(){
  if (x < 0.1) return '#f0f0f0';
  if (x < 0.2) return '#d3eadf';
  if (x < 0.3) return '#bce1cd';
  if (x >= 0.3) return '#9cd4b9';
})(); // call it immediately

Basically, the trick is that we're wrapping it in a function expression which converts it into an expression, this technique of an immediately invoked function expression is a useful trick in JavaScript.

Answer:2

I actually load an external JavaScript on my page this way: <script type='text/javascript' src='http://somedomain.com/somescript.php'></script> Note: it is a PHP file, but it produces ...

I actually load an external JavaScript on my page this way: <script type='text/javascript' src='http://somedomain.com/somescript.php'></script> Note: it is a PHP file, but it produces ...

  1. remove unwanted part of image online
  2. remove unwanted part of video
  3. remove unwanted part of image in photoshop
  4. remove unwanted parts from video online
  5. remove unwanted parts of a photo
  6. remove unwanted parts from a photo photoshop
  7. remove unwanted section breaks word
  8. how to remove unwanted part of video in vlc
  9. how to remove unwanted part of video in android
  10. how to remove unwanted part in photoshop
  11. process of removing unwanted part of an image
  12. process of removing unwanted part of an image is called

I'm trying to get count value from a function and want to store in outside of the function. var count; client.count({ index: 'employee', type: 'details', ...

I'm trying to get count value from a function and want to store in outside of the function. var count; client.count({ index: 'employee', type: 'details', ...

I have this code here: <!DOCTYPE html> <html> <body> <p id="demo">Display the result here.</p> <input type = "text" value = "ood" id = "txt1"/> <script> ...

I have this code here: <!DOCTYPE html> <html> <body> <p id="demo">Display the result here.</p> <input type = "text" value = "ood" id = "txt1"/> <script> ...

  1. string match javascript regular expression
  2. string match javascript boolean
  3. string match javascript case insensitive
  4. string match javascript example
  5. string.match javascript variable
  6. string match javascript mdn
  7. string match javascript group
  8. string match javascript search
  9. string match javascript number
  10. match string javascript function
  11. string exact match javascript
  12. string match array javascript
  13. partial string match javascript
  14. check string match javascript
  15. string match all javascript
  16. if string match javascript
  17. string match pattern javascript
  18. string match method javascript
  19. string match percentage javascript
  20. fuzzy string match javascript

I'm implementing a small service for my angularJS app that makes an http request to my server for a couple of text posts. Here's my current implementation: app.factory("postFetcher", ['$http', ...

I'm implementing a small service for my angularJS app that makes an http request to my server for a couple of text posts. Here's my current implementation: app.factory("postFetcher", ['$http', ...

  1. return object from array
  2. return object from arrow function
  3. return object from function javascript
  4. return object from map
  5. return object from promise
  6. return object from async task c#
  7. return object from function powershell
  8. return object from function python
  9. return object from function vba
  10. return object from async function
  11. return object from stream java
  12. return object from class python
  13. return object from list c#
  14. return object from async function javascript
  15. return object from function typescript
  16. return object from function r
  17. return object from arraylist
  18. return object from task c#
  19. return object from web api c#
  20. return object from filter javascript