This Week in JavaScript - 25 January 2016

Hello and welcome to ‘This Week in JavaScript’ — another curated collection of links relating to what’s new and exciting in the world of JavaScript. The complete list is tagged jsweekly. (Don’t forget to check out our weekly .NET and front end roundups too!)

And now for this week’s finds …


Getting started

  • Shuffle an Array - This and other tips from ‘A JS tip per day!’.
  • Use === instead of == - The == (or !=) operator performs an automatic type conversion if needed.
  • Keeping Promises With JavaScript - JavaScript, through its popularity and recent improvements, is increasingly becoming the web programmer’s best friend. And like all best friends, JavaScript keeps its promises.
  • Customizing Your Api.ai Assistant with Intent and Context - Patrick Catanzariti goes a step further with his coverage of API.ai and introduces “intents” and “contexts”, a way of teaching our AI assistants more specific actions that are personalized to our own needs

Learning more

Libraries

  • repo-copy - Git repository copy/compressor.
  • 5 jQuery.each() Function Examples - This function is one of jQuery’s most important and most used functions. In this article we’ll find out why and look into its details to see how you can use it.
  • 10 Best jQuery and HTML5 WYSIWYG Plugins - HTML5 WYSIWYG (What You See Is What You Get) editors are always high in demand. But there are now so many of them around that it’s hard to find the best ones. Ritesh Kumar sorts them out.
  • A Comparison of JavaScript Calendars and Schedulers - If you want to create your own calendar or scheduler but don’t know where to start, have no fear. This article’s aim is to help you with your decision.
  • CMS.js - CMS.js is fully client-side, Javascript site generator in the spirit of Jekyll that uses plain ol’ HTML, CSS and Javascript to generate your website.

ES6

Frameworks

  • Managing State in Angular 2 Applications - Managing application state is a hard problem. You need to coordinate between multiple backends, web workers, and UI components. Patterns like Redux and Flux are designed to address this problem by making this coordination more explicit. In this article, Viktor Savkin shows how you can implement a similar pattern in just a few lines of code using RxJS.
  • Non functional React.js - Is React.js really a functional framework?
  • Setting up SEO in an Angular app - As you probably know, the whole client-side MVC thing has its perks. But, it comes with a price: search engines usually won’t be able to scrape your site. Angular 2 should enable us to render pages on the server as well, which will help solve this. In the meantime, let us see what you can do with your production app today.
  • Cycle.js Fundamentals - Cycle.js is a framework where your app is described as a simple function taking an event stream as input and outputting an event stream.
  • Ember.JS 2.3 and 2.4 Beta Released - Updates to Ember.

#testing

  • React Testing Cookbook - In this course we will take a look at testing React applications. From getting setup and running tests, all the way through testing Redux enabled React applications.

##Everything Else


For more links like this and to keep up-to-date with the latest goings on in JS land, you can follow SitePoint’s JavaScript channel on Twitter.
Please PM us if you have anything of interest for the next issue or if there is anything you would like to see featured. Paul and Chris of Arabia.

Whew, that scared me enough to test. I think “conversion” is the wrong word to use

var ten_str = "10";
var ten_int = 10;
function compare_tens(ten_str, ten_int) {
  if (ten_str == ten_int){
    alert("=="); // ==
    alert("fn str " + typeof ten_str); // fn str string
    alert("fn int " + typeof ten_int); // fn int number
  }
}
compare_tens(ten_str, ten_int);
alert("str " + typeof ten_str); // str string
alert("int " + typeof ten_int); // int number

Not that I’m saying it’s a good idea to use == instead of === unless maybe you want them to test as true.

Not my own words I would hasten to add. Good to see someone’s paying attention though… :wink:

I just checked the PHP docs and it uses “conversion” in the heading. but “evaluated as” in the description.
http://php.net/manual/en/language.types.string.php#language.types.string.conversion

String conversion to numbers

When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

If the string does not contain any of the characters ‘.’, ‘e’, or ‘E’ and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.

I guess some of @ralphm 's idea of what constitutes correct grammar is rubbing off onto me.
I much prefer “evaluated as”

2 Likes

That is probably more descriptive of how JavaScript works when comparing variables of different types.

A more obvious example that JavaScript ‘evaluates as’ rather than ‘converts’ is:

function x(a) {
a = a || 0;
alert(a);
}

If conversion were to take place then the alert would display either true or 0.

There are situations where == can be used but === can’t - for example

n == +n

In this case the right hand side is converted to a number and then evaluated as the original variable type to see if doing both will change the value (which it will if the original value can’t be converted to a number). So you get an answer of true if n can be converted to a number. Using === would only return true if n is a number which is a different question.

Mind blown :scream:

While it’s admirable that JavaScript can achieve the above, and is great for code golf, I wouldn’t want that technique being used anywhere near the code that we use.

The code is much more clearly understood when something like an isNumber function instead, for example:

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}
if (isNumber(n)) {
    ...
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.