+++ description = "JavaScript and its quirks" title = "JavaScript" draft = false weight = 100 bref="JavaScript is a high-level, dynamic, weakly typed, prototype-based, and multi-paradigm interpreted programming language" toc = true script = 'animation' +++
JavaScript is a powerful language that can do almost any type of programming (functional, object, imperative). It's growth has made it the most popular language used today. Thousands of libraries and frameworks are available to simplify the task of coding. JavaScript can be run on the client or the server, eliminating the need to switch between different languages and making it the perfect web development tool.
- Variable
- Containers for storing data values. Data values can be any of the 6 primitive data types or an object.
- Primitive data types
- Boolean - "true" or "false"
- Null - the intentional absence of any value
- Undefined - indicates that a variable has not been assigned a value, or not declared at all
- Number - integer or float
- String - a series of characters inside double or single quotes
- Symbol - this data type is used as the key for an object property when the property is intended to be private, for the internal use of a class or an object type
- Object
- A non-primitive data type containing key-value pairs where the key is a string (also called “property name”), and value can be anything. Arrays, functions, dates, and errors are all of type object. Copying a variable referencing a primitive data type will pass the value to the new variable. However, because objects are a non-primitive data type, copying a variable yields a reference to the original object (NOT the value).
- Array
- An object containing an indexed list of values of any data type.
- Function
- An object containing a block of code designed to perform a particular task. It is executed when invoked with (). The function completes with a return statement. If the function has parameters, invoking it will pass an array named arguments to the function. Because functions are objects, they can themselves be passed as arguments to other functions. Passing functions as arguments enables asynchronous programming.
- Class
- An object containing a "blueprint" for creating many objects of the same "type". Classes contain a constructor function that is invoked when using the new keyword. Instances created through a class definition will inherit methods (functions) and properties of the parent class object through the prototype chain.
- Conditionals
- JavaScript has an "if/else if/else" statement, a ternary operator, and a switch statement.
- Loops
- A block of code useful for running the same code over and over again, each time with a different value. Often this is the case when working with arrays or objects. Use a for loop for looping every item in an array. "For/in" loops through the properties of an object. "While" loops through a block of code while a specified condition is true.
- "use strict"
- JavaScript's strict mode that eliminates some silent errors by changing them to throw errors. For example, an error will be thrown when using undeclared variables.
- "this" keyword
- As soon as the program runs, "this" is defaulted to the global object. As the program executes, "this" will change to the object it belongs to, which might be different each time the function is called. In a browser event, this refers to the element that received the event. There are call(), bind(), and apply() methods to set the value of a function's "this" regardless of how it's called. There are also arrow functions which don't provide their own this binding (it retains the this value of the enclosing lexical context).
- Closures & Scopes
- A closure is the combination of a function and the lexical scope within which that function was declared. Variables defined inside a function are not accessible from outside the function. Variables defined outside the function are accessible inside the function.
- Hoisting
- JavaScript's default behavior of moving declarations to the top. This occurs during the compilation step when the JavaScript engine creates the program's scopes before the interpreter executes the code.
- Floating points
- Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers which means that 1/10 in base 2 is a repeating decimal that will be rounded.
- ECMAScript versions
- A specification to standardize JavaScript with updates to the language every year. As a result, the latest features of JavaScript might not be able to run in older runtime environments. Fortunately, tools like Webpack and Babel can transpile JavaScript. The transpiler converts the latest versions of JavaScript into earlier versions like ES5 that are compatible with almost all runtimes.
- Type coercion
- The process of converting values from one type to another (such as string to number, object to boolean, and so on). See chart below for examples. Use === to check for equality AND type. Many developers prefer a statically typed language for issues like this. Using TypeScript adds static typing and other useful features before being transcompiled into JavaScript.
- Array methods
- All the functions you will ever need to manipulate array data
- JavaScript Libraries
- Awesome browser-side JavaScript libraries
- JavaScript Snippets
- A huge collection of useful functions
- Design patterns
- Design patterns for commonly occuring themes in software engineering
- MDN JavaScript docs
- The best tutorials and references for JavaScript on the web
- TypeScript in 5 minutes
- Official documentation from TypeScript