C# vs JavaScript: Introduction

by Nicklas Envall

This post is the introduction to the new "C# vs JavaScript" series on this site. We'll be comparing JavaScript to C#. The subsequent posts will make sense in isolation, but this introduction sets the tone for all of them. In the series, we'll look at:

  1. Type System
  2. Strings
  3. Numbers
  4. Arrays
  5. Lambdas
  6. LINQ

And there's more to come. I'll update this list as we go.

JavaScript & Browser

The circumstances that JavaScript got created under caused it to become a misunderstood language. Contrary to what many believe, it's not a poor attempted copy of Java, and it has never been. Because from the start, the web needed a language embedded into browsers for scripting, and Scheme was supposed to be that language. Instead, after various events, a decision was made that a new language where to be created, which would be less like Scheme and more like Java. Consequently, the new language inherited its syntax from Java and the confusing name JavaScript. Admittedly it might seem like I just contradicted my opening statement. But JavaScript was still influenced by Scheme and Self in the end. Its creator Brendan Eich snook other concepts into the language rather than just copy-pasting Java. JavaScript also needed to be easier to learn than Java and requires another mindset than you would have with Java. Nevertheless, today it has both differences and similarities to a language like C#, which itself is heavily inspired by Java, by the way.

More technically speaking, JavaScript is a weakly and dynamically typed scripting language with lexically scoped first-class functions. It supports event-driven, functional, and imperative paradigms. Here's a concise list of JavaScript:

  • JavaScript is object-oriented (prototype-based)
  • JavaScript supports imperative, functional, and event-driven styles.
  • JavaScript treats functions as first-class citizens.
  • JavaScript is class-free, and objects inherit directly from other objects.
  • JavaScript is influenced by Scheme.
  • JavaScript's syntax is based on Java and C languages.

It being a scripting language means that it operates in a host environment (runtime) that gives it the means to communicate with the outside world. In fact, JavaScript itself has no notion of input or output but depends on an environment. For example, the browser acts as the host and provides objects to the language such as XMLHttpRequest. Subsequently, it has become a general-purpose language with the help of other environments like Node.js that enables server-side programming with JavaScript. However, it originally was intended to be used in browsers, and we'll be focusing on JavaScript in the browser environment.

More importantly, JavaScript code doesn't require a traditional build-step. It does not get stored in a compiled version that later gets served to the browser. Instead, each modern browser implements their JavaScript engine that uses JIT-compilers that translates the code into machine code on the fly. That said, JavaScript itself is often a compile-target because it's common to write in different languages such as CoffeeScript, Dart, Elm, ClojureScript, or TypeScript and then transpile that code into JavaScript. Lastly, as a short side note, the creator of TypeScript is Anders Hejlsberg, who also formed a team to build C#. You can read an introduction to TypeScript here.

C# & .NET

C# has received criticism for being a poor copy of Java. But unlike JavaScript that would act as a complement to Java, C# was intended to replace Java in the Windows platform. Consequently, C# and Java have many similarities, and some developers even go as far as saying that it's the same language with different names. Although note that C# development has been pretty much exclusive to Microsoft environments up until recent years. However, Microsoft is nowadays focusing on making C# development more cross-platform, like Java, and as a result, has gained new attention from developers.

More technically speaking, C# is object-oriented (class-based) and strongly and statically typed. It's a general-purpose language that's multi-paradigm so it supports both imperative and functional styles. Here's a concise list of C#:

  • C# is object-oriented (class-based).
  • C# has strong & static typing.
  • C# enforces type-safety at runtime & compile-time.
  • C# has a unified type system: all types derives from Object.
  • C# can treat functions as values with delegates.
  • C# supports FP to some extent with LINQ, lambda, and immutability.

The relationship between C# and its runtime (.NET) works differently than how JavaScript works with its runtime. Because, often when we talk about platforms, we may refer to Windows x86 or Linux x64, which would be correct. But, in the .NET world, things get a bit more complicated than that. Firstly, we have a so-called .NET Standard which, is an interface that unifies .NET platforms.

So what is a .NET platform?

Well, .NET Core, .NET Framework, Mono, or Xamarin are all examples of .NET platforms. Confusingly enough, all of them are both frameworks and platforms. To clarify, they are frameworks because they provide a library but also platforms since they provide a runtime and compiler. So to summarise, a .NET framework/platform will provide both a Common Language Runtime (CLR) and Class library.

Unlike JavaScript, with C#, we are required to pre-compile our code. We use a .NET compiler to compile C# code to a platform-independent intermediate language (IL). Then, that compiled version of the code gets saved as a .NET assembly file. Later, when we want to run our programs, we must have a .NET platform (like .NET Core) installed on our computer. The .NET platform is required because it will provide a .NET runtime that acts as a runtime environment for our C# applications (assemblies). Lastly, the runtime will use a JIT-compiler to compile the intermediate code into machine code specific to the architecture and platform that you target (like, x86 or x64).

Even so, let's not avoid the fact that ".NET Framework" is a confusing name. Because as we just covered, it's both a platform and framework. Though it's a reality that we must live with, I suppose. But we shouldn't let that dampen our spirits. The reason being, that Microsoft has recently shifted focus on making .NET more open-source and cross-platform with, for example, .NET Core. Thus, we can run on different operating systems like Linux, Windows, and macOS.