JavaScript has come a long way since its creation in the mid-90s. What started as a client-side scripting language can now be used as a server-side language as well through the introduction of frameworks like Node.js, essentially making it the language of the internet. It has been ranked as the most commonly used programming language for the 8th consecutive time, it is used to build roughly 95% of the world’s websites (1.5 billion) and it is one of the most in-demand skills in the job marketplace.
One of the reasons JavaScript has become so ubiquitous is because it provides unparalleled freedom which in turn means it comes with its own set of quirks. From its inception to its more recent developments here are some things you might not have heard about our favorite language.
1. The Origins: how Javascript started?
In 1995 Brendan Eich joined Netscape with the intention of putting a language called Scheme “in the browser”. His superiors had something else in mind and wanted something that more closely resembled the syntax of Java. With the looming Beta release of Netscape 2.0 he completed the first version in just 10 days. Certainly JavaScript looks very different from that first version, but to think its main elements were written in just 10 days is nothing short of amazing. This “first draft” was called Mocha but it quickly changed to Livescript and later to Javascript.
2. JavaScript vs Java
There is often confusion between these two languages due to their very similar names. But why do their names sound so alike when the languages themselves have very little in common? That’s where the marketing teams come in. According to its creator:
“... It was all within six months from May till December (1995) that it was Mocha and then LiveScript. And then in early December, Netscape and Sun did a license agreement and it became JavaScript. And the idea was to make it a complementary scripting language to go with Java, with the compiled language.”
3. Null is an Object
Common sense dictates that null means no value or to put it in more precise terms the total absence of meaningful value. But in JavaScript null is considered an object.
alert
(typeof null)
// alerts 'object'
Even though many consider this to be a bug (one which if fixed would have disastrous consequences to the backward compatibility of the language) it's actually intended behavior. As previously stated JavaScript was supposed to interconnect with Java. This resulted in a mismatch between a language that has both undefined and null (JavaScript) and one that only has null (Java). In its own creator words: “JS was supposed to interconnect with Java, including object refs that could be null. Blame Java yet again.” Hence when checking for type object it’s always better to remember to check for our old friend null.
typeof
yourVariable === 'object' && yourVariable !==
null
4. NaN is a number
While this one is not exclusive to JavaScript it’s still worth mentioning the fact that NaN which literally stands for Not a number is considered a number.
alert(
typeof NaN
) // alerts 'Number'
According to the law of the land, ECMAScript: “set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity”. It gets better :
alert( NaN === NaN ) // evaluates false
NaN refers to a value that cannot be represented within the limitations of the numeric type which does not mean that when encountering two NaN they are the same. Even though their value can not be represented it might be different non-representable values.
5. Semicolon to start
In programming languages, the semicolon is used to denote the end of a complete statement and is therefore mandatory. In JavaScript it is not, due to the magic that is automatic semicolon insertion or ASI. ASI is a set of rules that will automatically determine whether a semicolon will be inserted or not. This means the use of the semicolon is optional, and not only that, one can even write the semicolon at the start of the statement.
;let a = 5
6. Dot Notation vs Bracket Notation
One of the basics of JavaScript dictates that in order to access an object’s properties one can use both dot notation as well as bracket notation.
const obj = {
name : 'carlos',
age : 12
}
alert(obj.name) // alerts carlos
alert(obj["name"]) // alerts 'carlos'
One reason to use one or the other is that bracket notation allows for the use of variables.
const obj = {
name : 'carlos',
age : 12
}
const str = 'name'
alert(obj.name) // alerts carlos
alert(obj[str]) // alerts carlos
Another reason is that dot notation only accepts alphanumeric values (and _ and $), it also can not start with a number and it can not have spaces. Bracket notation, on the other hand, accepts anything in the form of a string
const obj = {
name : 'carlos',
age : 12,
"12ss jk_#": 7
}
const str = 'name'
alert(obj.12ss jk_#) // syntax error
alert(obj["12ss jk_#"]) // alerts 'carlos'
7. Function arguments
Inside a function there is a special array-like variable called arguments which contains all the parameters that are passed to said function when it is called.
function argumentsTest () {
alert(arguments.length)
}
argumentsTest(1,2,3,4) // alerts 4
It is an array-like and not an actual array because even though it provides length and indices, you can’t use regular array methods like push or pop.
function argumentsTest () {
arguments.push(5)
}
argumentsTest(1,2,3,4) // TypeError: arguments.push is not a function
8. Optional chaining
This new feature is becoming fundamental in order to write cleaner and more concise code. When trying to access deeply nested properties inside an object there was always the worry of trying to access a property that did not exist. This would result in the dreaded ERROR which would mean the code would stop running. With this new optional chaining there is no need to worry as if the property does not exist it will return undefined and the code will still be able to run.
const obj = {
person : {
name : "carlos"
}
}
console.log(obj?.person?.surname)
While it might sound daunting, becoming a JavaScript developer takes shorter than you might think. Attending a web development bootcamp takes as little as 9 weeks! With over 4.5 billion people using the Internet on a regular basis, it is safe to say that there is a lot of demand for coding in JavaScript and it’s only getting bigger. Becoming aware of these lesser known features will help any developer stand out from the crowd and take their JavaScript knowledge to the next level.