Easy Peasy Javascript Squeezy

30 Aug 2023

C++ is the one who throws the lemons and makes you squeeze them

Remember that phrase when life gives you lemons? Yeah, it rained lemons when I was starting out with Introduction to Computer Science courses at the University of the South Pacific. Our professor wanted us to start out with C++ so that we can experience the challenging programming language so that when we dive into others, it will be easy. In the first section, we explored variables, variable assignments, operators, input/output, data types, functions, conditional statements, scopes of functions and variables, arrays, structs and classes. Well, looking back, I think it was a drizzle of lemons because I was comfortable with the topics and that the assignments were pleasant. I thought, if this is C++, sign me up!

When we went more in depth in the second section, that is when the storm of lemons poured heavily. Pointers were my greatest enemy. Pointers and variables, pointers and arrays, pointers and objects, pointers to functions, pointer this, pointer that. Then we had all these other methods in relation to memory management such as destructors, virtual functions, deep copy, overloaded assignment operators and so on. Then we started getting involved into data structures such as linked lists and algorithms that implement these data structures. All of these were difficult to grasp at first and goes to show how complex the C++ language is. It was a deep learning curve for me.

Will Javascript add oranges to the list of what is being thrown?

As I transferred over to UH Manoa, I was syntactically confused when the teacher for my ICS 311 course would throw up slides and its code did not look like C++ (more like I was thinking about the multiple errors the compiler would shout at me if I had written that piece of code). I found out from my colleagues that UH Manoa uses Java in their Introduction to Computer Science courses. I then decided to take a crack at learning Java. They were similar in many ways but the biggest difference between Java and C++ was the automatic garbage collection making memory management so much easier. Hooray to no more using pointers explicitly.

Therefore, when I learnt at the start that we were going to begin with the module on Javascript, I was expecting it to be like a sibling of Java. After taking the Basic Javascript module on free code camp, the first major difference I noticed was the “loose” typing. In C++ and Java, we have to declare the data type of the variables we use, as it enforces strong typing. Not only at the initial declaration but also when passing it into functions or returning from functions. In Javascript, the variables are not bound to any type when declared and that is inferred when assigned.

The next difference was objects. In Javascript, you can modify objects directly by adding properties or deleting them whereas in C++, it is not as straightforward and may need additional member functions in the class to handle these modifications. Another difference is the push, pop, unshift, and shift operations on an array. Push and pop are also on C++ for stacks and vectors whereas to achieve a similar effect of unshift and shift in C++ you have the function like insert and erase for vectors.

ES6 says no throwing business at all!

Trekking through the new features of ES6 on the freecode camp course is where I can start to pick out some uniqueness of Javascript that stands alone from C++. I think well of how it simplifies or makes things more flexible. Say for example, the arrow functions, with lesser syntax, it makes code short and easy. Then there is the destructuring assignment, which makes accessing arrays and objects more readable and quick especially if dealing with big arrays or objects with many properties. Moreover there’s the spread and rest operators that makes copying over elements a breeze.

Towards the end of the course, we touched on writing scripts, importing and exporting them and in these techniques are shared on how to transfer pieces of code between files well. In addition, we touched on writing promises and this reminded me of a client/server system whereby the client meets the promise and we resolve their request or they do not and we reject, handling each case. Overall, I am beginning to make connections of Javascript to Web Development with just the small insights from the ES6 module.

Key Takeaway

Largely, Javascript is more gentle and forgiving in terms of syntax in comparison to C++. It is also less complex and we do not need to know a lot of low level implementation. With C++, it is more rigorous and you are expected to be vigilant.