Hello JavaScript!
Sunday, Jan 5, 2020
JavaScript
JavaScript
I was a little bit hesitant to throw JavaScript into the mix at this time (while learning C). Primary concerns were:
- not a good idea to focus on more that one language at a time
- do not want to work outside of command line (troubleshooting in browser?)
Well the problem is that I am also learning about algorithms concurrently and apparently more work is needed in C as there are no in-built data structures (besides the bare bones fixed-length array). It will take me some time to learn to create custom data structures in C. JavaScript however has implementation of dynamic arrays that I can start using to learn algorithms. The concerns I mentioned earlier are not that big since:
- High level syntax (loops, conditionals, functions) is close enough to C that there is no additional cognitive load
- I can run JavaScript from command line using node (yay!)
Here we go
console.log("Hello, JavaScript!");
console.log();
// function call
greet("World");
console.log();
// any variable type defined using var
var foo = 5;
var bar = "Hello";
// arrays
var nums = [1, 2, 3, 4];
console.log(foo, foo * foo, Math.sqrt(foo));
console.log();
foo++; // var incrementation
console.log(foo, bar, nums);
console.log();
// conditional
if (foo > 5 && bar == "Hello")
console.log(bar, foo);
console.log("Number Square");
console.log();
// loop
for (var i = 0; i < nums.length; i++)
console.log(nums[i]," ", nums[i] * nums[i]);
// functions
function greet(name)
{
console.log("Hello, "+ name + "!");
console.log("How are you today?");
}
Output
Hello, JavaScript!
Hello, World!
How are you today?
5 25 2.23606797749979
6 Hello [ 1, 2, 3, 4 ]
Hello 6
Number Square
1 1
2 4
3 9
4 16