C# vs JavaScript: an overview

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.

Novice · JavaScript · Updated

After this lesson you can

  • Explain where JavaScript runs, and the main ways it differs from C#

Key takeaways

  • JavaScript runs in a JavaScript engine: there's one in every web browser, and Node.js runs it outside the browser.
  • JavaScript is standardized as ECMAScript, with a new version every year. ECMAScript 2015 (ES6) brought let, const, arrow functions and classes.
  • C# is compiled, so many errors show before it runs. JavaScript runs straight away, so many errors show only when that line runs.
  • C# types are static and never change; JavaScript types are dynamic, so a variable can hold any type and change it.
  • Try JavaScript in the browser's Console (F12) or with node app.js; console.log(...) prints, like Console.WriteLine(...).

JavaScript will feel familiar. It has braces, semicolons, if, for, while, return, and the same operators as C#. But it's a different language, with different rules underneath, and the differences are what this chapter is about.

A short history

In 1995, Brendan Eich at Netscape wrote a small language in ten days to let web pages do more than sit still. Java was the exciting new language of the time, so the new one was named JavaScript to borrow some of its fame. The two are no more related than car and carpet.

In 1997 JavaScript was standardized, under the official name ECMAScript. Versions are named after the year: ECMAScript 2015 (also called ES6) was the big one, bringing let, const, arrow functions and classes. A new version comes out every year, and today's browsers support them all.

Where JavaScript runs

C# runs on .NET. JavaScript runs inside a JavaScript engine, and there's one in every web browser. That's what makes it special: it's the only language every browser runs, so every interactive web page uses it.

It also runs outside the browser, in Node.js, which takes the engine from Chrome and adds what a program needs to read files and talk to the network. You installed Node.js when you set up your computer.

You can run the examples in this chapter two ways:

  • In the browser: press F12, open the Console tab, type JavaScript and press Enter.
  • With Node.js: save the code as app.js and run node app.js in a terminal.

Either way, console.log(...) prints, like Console.WriteLine(...):

console.log('Hello from JavaScript');
console.log(2 + 3);
Hello from JavaScript
5

The big differences

C# JavaScript
Runs on .NET A browser's JavaScript engine, or Node.js
Before it runs Compiled: errors found before the program starts Read and run straight away: many errors found only when that line runs
Types Static: every variable has a type that never changes Dynamic: a variable can hold any type, and change it
A program starts At Program.cs Wherever the browser or Node.js loads the file
Classes Everything lives in classes Functions and plain objects come first; classes are optional

The next lessons take these one at a time, always starting from the C# you know.

Check yourself

1 question to try on the lesson page.

So what

JavaScript borrowed C#'s look (or rather, C and Java's), not its rules. Read it with fresh eyes, and you'll find a small, flexible language that runs everywhere, starting with every browser on earth.

Key terms

Cheat sheets