Introduction

The purpose of this technical document is to give a high-level comparison between Javascript and C#. Both languages have their merits and shine in different use cases. The aim is giving the reader a place to begin researching the topic an point them towards further avenues of inquiry.

Brief Language History:

Hello World code examples:

C#:
        
// A Hello World! program in C#.
using System;
namespace HelloWorld
            {
              class Hello
              {
                static void Main()
                {
                  Console.WriteLine("Hello World!");
                // Keep the console window open in debug mode.
                  Console.WriteLine("Press any key to exit.");
                  Console.ReadKey();
                }
              }
            }

        
      
Javascript:
        
            //This code prints to console and creates a pop up alert with an ok in a web browser.
            console.log("Hello, World!");
            alert("Hello, World!");
        
      

Compilation

In the past, Javascript was more strictly an interpreted language. While C# was built from the beginning to be a compiled language.

Just-In-Time(JITS) compilers now exist which compile Javascript into machine-code in web browsers for instance.

Typing

C# is a statically and strongly typed language. It can make it a little harder for new programmers to get started and just jump in. However, C#'s strong typing helps new and veteran programmers alike develop good programming habits. C# isn't as forgiving as Javascript.

With some restrictions, C# does allow some flexibility with implicitly typed local variables, but it isn't like the 'wild west' of Javascript.

Javascript is a dynamically typed language, which gives some flexibility, especially for smaller projects. Javascript's dynamic typing, while increasing flexibility, can also make debugging harder, and allow bugs to creep in where C# will not allow it.

Variable Declaration Code Examples:

Javascript:
            
//notice the lack of ints floats and other designations that you might see in C# or Java when declaring variables.
var nameFruit = "banana";
var quantity = 12;
var isYellow = true;
var weight = 2.2;
var unit = 'Oz'; // '' denotes string not char in JS unlike strongly typed
            
          
C#:
            
//please note for this to be compiled you would need using statements, a namespace, and a main function like so:
using System;
namespace variablesFruit {
  class Program {
      static void Main(args) {
    //Variable Declariations
          string nameFruit;
          int quantity;
          bool isYellow;
          double weight;
          char unit;

          nameFruit = "banana"; // string initialized
          quantity = 12; // integer initialized
          isYellow = true; // boolean initialized
          weight = 2.2; //double float point initialized
          unit = 'O' //char

    //A one line declaration as an alternative:
          string nameFruit = "Strawberry";
            }
          }
        }
            
          

Components

Javascript and C# use various components and libraries to query data.

Javascript has various seperate libraries one can use to work with data

C# has a native .NET component called LINQ which works with many different databases and frameworks.

Threading

C# can natively run multple threads. Javascript was built as a single-thread language.

There are ways to work around Javascripts limitations with respect to multithreading using "web workers"

Here is a code JS snippet of a web worker in action:
        
// worker.js
self.addEventListener('message', function(e) {
  var message = e.data + 'to myself!';
  self.postMessage(message);
  self.close();
}
        
        
Source: From Max Peng on Medium (check out appropriate links om the references section for more information on web workers.)

Overloading

Javascript does not natively support function overloading. C# allows function overloading natively. You can name functions the same name as long as they have different signatures or number of parameters.

Function overloading is central to many Object-Oriented programming languages, Javascript has limitations in this area.

There are some workarounds in Javascript to mimic the OOP concept of function overloading. The example provided below isn't overloading in the true sense, but it's clever.

Function Overloading Workaround in Javascript:
          

function sum() {
  var msg = 0;
//function parameters are actually stored as an array-like structure called arguments.
//The arguments object is automatically available inside any function
//We can get the legth of arguments
//if we don't pass any parameter, then below code executed arguments.lengh ==0
if (arguments.length == 0) {
    msg = "You haven't passed any parameters
"; document.write(msg); } //if we pass single parameter, then the arguments length is 1 else if (arguments.length == 1) { msg = "Please pass atleast two parameters
"; document.write(msg); } else { var result = 0; var len = arguments.length; //since arguments acts like an array, we can loop through it. for (i = 0; i < len; i++) { result = result + arguments[i]; } document.write(result + "
"); } } sum(); //Calling function with no parameters sum(1); //Calling function with one parameters sum(1, 8); //Calling function with two parameters sum(1, 2, 5, 6, 67, 8, 8); //Calling function with multiple parameters
Source: Vipin Sharma via c-sharpcorner.com

C# Method Overloading snippet from Microsoft Docs:

          
            public static class Console {
    public void WriteLine();
    public void WriteLine(string value);
    public void WriteLine(bool value);
    ...
}
          
        
Source: Member Overloading from Microsoft Documentation

C#'s native overloading support is a big help for productivity and reusability. JS is flexible and scrappy.

References