types

Matthias Reuter's picture

Handling the unexpected - Type safe functions in Javascript

Javascript is a weird language. Great but weird. Take functions for example. You cannot only pass any type of arguments, you can also pass any number of arguments. This may be quite disturbing, especially for Java developers. Recently I had a discussion with a Java developer who complained about missing code assistance in Javascript, by which he meant no hint about the type of an argument.

This is of course due to the dynamically typed nature of Javascript. While in Java you denote the expected type of a parameter, and it's the caller's duty to pass the correct type (even more, you cannot pass a different type), in Javascript it's the function's duty to handle the given parameters and do something reasonable with unexpected types. The question arises: How do you write type safe functions in Javascript? Let me explain this by an example implementation to calculate the greatest common divisor of two numbers.Read more

Matthias Reuter's picture

All about types in Javascript - Explicit type conversion

This is the third part of a series "All about types" in Javascript.

  1. The basics - types and general rules of conversion
  2. Automatic type conversion
  3. Explicit type conversion
  4. Type detection

Explicit type conversion

When I wrote about automatic type conversion I told my story of testing if "0" really converts to true, which I did by comparison to true. That was wrong, as I found out, but what is the right way? One possibility is

if ("0") {
  alert("true");
}
else {
  alert("false");
}
Read more

Matthias Reuter's picture

All about types in Javascript - Automatic type conversion

This is the second part of a series "All about types" in Javascript.

  1. The basics - types and general rules of conversion
  2. Automatic type conversion
  3. Explicit type conversion
  4. Type detection

Automatic type conversion

You have seen it before:

var element = document.getElementById("someId");
if (element) {
  // do something
}

That's an automatic type conversion. The if-statement expects a boolean value, and if the given expression does not return one, the result is converted. document.getElementById either returns an object or null. Null is converted to false, any object to true. That's why constructions as the above work.Read more

Matthias Reuter's picture

All about types in Javascript - The basics

Javascript is a loosely typed language. That means a variable x may now hold a number, two lines down a string and another ten lines down an HTML element. What might be confusing is that Javascript often converts types automatically. The rules behind automatic type convertion sometimes are - well, surprisingly abnormal. Did you know that

"0" == false

evaluates to true and why?

Programmers from other languages such as Java often find dynamic typing challenging, and therefore try to twist Javascript to help avoid typing problems like that:

function isString(obj) { return typeof(obj) == 'string'; }
function isNumber(obj) { return typeof(obj) == 'number'; }
function isBoolean(obj) { return typeof(obj) == 'boolean'; }
function isFunction(obj) { return typeof(obj) == 'function'; }
function isObject(obj) { return typeof(obj) == 'object' || isFunction(obj); }
Read more

Syndicate content