This blog post is for developers who already know how to write software and want to learn about JavaScript. I am not teaching you how to write software because I assume you might code in a different language. I also assume that even JavaScript-Ninjas enjoy some drop in conversations of topics they might or might not know.
Most of those articles will cover the must-learns in order to be a proficient JavaScript developer. Things for you to become more efficient in your style of programming. My idea is to split it into several parts and to make it as simple as possible. Some of the topics will include:
Javascript – Table of Content
There are a lot of gotchas in this language that you need to know about a this elastic band called JavaScript. There are a lot of videos out there that teach the theory of JavaScript, some of the underlying mechanics but once you get deeper with Javascript it seems like you don’t really get to know everything. But there are in fact bullet points you will need to master your next job interview.
JavaScript opens the door wide open for you to accommodate the not so appropriate patterns and is riddled with incremental surprises you need to understand.
Javascript – why learn about it?
So, why should you learn about JavaScript? I tell you why, it is becoming a very ubiquitous programming language for the web. It is everywhere. Its not just on the fronted but also on the backend with frameworks such as NodeJS. There are a number of libraries that expose JS to a multitude of devices. For example apache Cordova for mobile phones, or Electron for cross platform desktop apps. Those tools can help you implement a prototype really fast!
Javascript – a dynamically typed language
JavaScript is a dynamically typed language. That means you can create a variable with var and assign it an object of any type, such as a string, or a number, etc. Don’t forget, everything in JavaScript is an object. Because it is dynamically typed, it is completely legal to overwrite an instantiated string variable with a number value, like so:
var numberVar = 2; console.log('2' - 2); // = 0 console.log('2' + 2); // = 22 console.log(parseInt(numberVar) + 2); // = 4
This can be quite frustrating when it comes to write JavaScript code. The reason as why this is happening is because it inverse the type of variable at runtime. It sees a plus for concatenation of strings and inverse a number as a string. One way to get around it is to be clear about your types and parse the hell out of your variables. Based on the last example, parseInt() parses a string argument and returns an integer of the specified type.
This is one way to do it. Another is to make use of typeof. This operator returns a string indicating the type of an unevaluated operand. Here an example:
/** * @type {*[]} */ var person = [{ "firstName": "Michel", "lastName": "Herszak", }]; /** * Most developers would expect a type of array, right? Wrong. */ console.log(typeof person);
This will commonly get people. Why – you ask? typeof actually gets you an instance of a variable of an array. Your tack away: Don’t do that for instances, do it for primitives. To get around this issue use another keyword called instanceof.
console.log(person instanceof array);
If you want to dynamically determine the type of your variable make use of instanceof for instance variable.
So, all of JS types are actually objects. That means you can make use of its methods and properties. A property could be length. It seems a bit odd, but useful non the less.
console.log("Michel".length); // 6
We have just covered the first part of this series of pattern and anti-pattern. Lets move on and talk about patterns!
Email – michel.herszak@gmail.com
Twitter – @MHerszak (twitter.com/MHerszak)
Want to know about the way in which I work? Great, you can get started with an overview of just how I approach a project right here.