JavaScript for C# Developers
JavaScript runs in every browser. You will see how it compares with C#, from dynamic typing to functions, arrays and arrow functions.
Updated
The JavaScript Runtime
- The JavaScript Runtime (Novice): What a JavaScript runtime is, and how the browser loads a page's HTML, CSS and JavaScript and runs the script.
C# vs JavaScript
- C# vs JavaScript: an overview (Novice): JavaScript was made for browsers in 1995 and is standardized as ECMAScript. It looks like C#, with braces, if and for, but it's a different language. It runs in every browser, and in Node.js outside one.
- Static vs Dynamic Typing (Novice): In C#, a variable's type is fixed when it's declared. In JavaScript, variables have no type; values do, and a variable can hold any of them. Declare with const by default, let when the value changes, and never var.
- Working with Functions (Apprentice): A JavaScript function is declared with the function keyword, and has no return type and no parameter types. A missing argument is undefined, an extra one is ignored, and a function with no return gives undefined. Functions are values, too.
- Arrays (Apprentice): A JavaScript array works like a C# List<T>. It grows and shrinks with push, pop, shift and unshift, and has length, includes and indexOf. Its filter, map, find and reduce methods do the jobs of LINQ's Where, Select, First and Sum.
- Arrow Functions (Apprentice): An arrow function is JavaScript's lambda, written (a, b) => a + b, almost exactly like C#. One parameter needs no parentheses, a body in braces needs a return, and an object returned on its own needs parentheses around it.
- Summary (Apprentice): Compare with === and !==, never == and !=, which convert types first. In a condition, false, 0, an empty string, null, undefined and NaN count as false, and everything else as true. A cheat sheet of C# and its JavaScript equivalents.
Exercise: Cookies and Pizza
- Exercise: Cookies and Pizza (Apprentice): Plan a party in JavaScript. Write functions with default parameters for cookies and boxes, then use filter, map, find, sort and reduce with arrow functions to pick pizzas, price an order and describe it, with tests run by Node.