Going From JavaScript to C#: Syntax Similarities and Differences

For the past few weeks, I have switched from coding in Javascript to learning a new language: C#.

C# was developed by Microsoft to be its primary computer coding language. It is an object-oriented, general-purpose programming language. It is also a type-safe language that allows a developer to build a wide array of applications that run on .Net frameworks.

Now, C# and JavaScript have very different applications, and the references below discuss when to use either and their strengths /weaknesses. While learning C# I wanted to discuss the initial similarities and differences in syntax that I noticed:

Similarities

for (int i = 1; i < 10; i++) 
{
Console.WriteLine(i);
}

While keywords like “int” or methods like “Console.WriteLine” may not be familiar from JavaScript, the for loop doesn’t look very different at all! It’s very easy to understand the code while having no experience in C# because of the syntax similarities.

Differences

  • The most notable difference when you start to code in C# is that you now have to explicitly define your data types before your variables. This is because C# is a “Statically-typed language” as opposed to JavaScript’s “Dynamically-typed language”. Not only do you have to declare the variable type when you declare the variable but you can not change the type of the variable throughout your code.
  • C# is strongly typed, while JavaScript isn’t. This means that there are restrictions between conversions between types. While we could do “2” + 8 in JavaScript with no issues, C# will not be so happy.
  • C# requires a compulsory semicolon at the end of a line. I’m lucky I started learning JavaScript when “;” wasn't required at the end of a line since I almost always forget one or two!
  • Capitalizing the built-in functions like array.Length instead of array.length.
  • Javascript doesn’t have an array datatype since its just really an object under the hood. However, C# supports the array data type. Additionally, in C# your array elements have to all be the same type whereas in JavaScript they do not.

References

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio