javascript
Introduction
engines
- V8 : chrome and opera (also nodejs)
- spidermonkey : firefox
- Trident or Chakra : IE
- ChakraCore : Edge
- Nitro or SquirrelFish : Safari
higher languages
Mostly turned into javascript with a compiler:
- Coffeescript : syntactic sugar mainly
- Typescript (Microsoft) : adds strict typing and more. Angular uses it
- Flow : adds data typing
- Dart : completely different language, but it can compile to javascript
links
ECMA-262 specification : visit Mozilla reference : visit.
Two sites where you can check browser compatibility :
And of course javascript info, an easy readable site :
require vs import
A javascript module is a .js file containing code, like a javascript library. You can import that code using different methods.
In NodeJS require() is a built-in function. require() is the CommonJS method of importing. NodeJS follows this method.
- It reads the file.
- Executes it.
- Returns the export object (module.exports = { something })
However ES style import() and export() are also available in nodejs.
import() and export() statements refer to ES modules and cannot be used for .json files etc. Just js/ts. EM modules
If you use .mjs (module js) nodejs import()s it as a EM type module. If you use .cjs extension (CommonJS js) nodejs require()s it as CommomJS module.
Though ES modules are more dynamic, commonjs modules are more straightforward and predictable.
So i will go with NodeJS and use require / modules.export as default.
in-browser scripts
Warning
The script tag does not need type= or language= anymore, you look old when you use them, src= is still vital though
Scripts can either contain code or a src= attribute, not both.
| script tag | |
|---|---|
syntax
semicolons
Semicolons are mostly not needed, but there are situations where you get unexpected behavior, like :
console.log("Error") evaluates to undefined, so undefined[1,2,4] gives an error.
Important
Just always use semicolons like in C, it is also widely adopted by the community.
By the way if you remove the "Error" line above, it will still give a slightly unexpected result :
But this is because .forEach passes 3 arguments : .forEach(function(value,index,array)) , and console.log() just prints them all.
comments
Just like in C :
use strict
You should always try to put 'use strict' in the beginning of new javascript code or at least try to get older code working with it. It means that it follow the modern rules for javascript.
Warning
use strict must be on the first line, it is just ignored anywhere else.
Important
It also means you might have to disable it for older code as newer version of javascript emerge.