on
29-Jun-2015
09:24
- edited on
05-Jun-2023
22:28
by
JimmyPackets
Node.js is an open source runtime environment for server-side and network based applications. At it's core, Node.js uses the Google V8 JavaScript engine to execute client code and a large portion of the core basic modules are written in JavaScript. Since, for all practical purposes, writing Node.js applications is writing JavaScript applications, it makes sense to try to understand the gist of what JavaScript is.
JavaScript is primarily a client-based dynamic scripting language most commonly used within web browsers as client-side scripts to interact with the user, browser, and communicate asynchronously to servers.
If you have been part of any web-based development, odds are you have worked with JavaScript in one form or another. In this article, I'll focus on the aspects of JavaScript that are relevant within the Node.js environment.
Node.js has only a few core object types including boolean, number, object, and string. There are three complex types consisting of the array, function, and object. And, finally, null and undefined are special objects that are treated in their own way.
> typeof 10; 'number' > typeof "HI"; 'string' > typeof function() { } 'function
While Node.js does support the const keyword, is it not widely used. A standard practice is to use all upperscore characters when defining a constant.
> var HOURS_PER_DAY = 24;
All numbers in JavaScript are 64-bit 754 double-precision floating-point numbers. For numbers that can be expressed in 2&53 bits, their type behaves much like an integer in any other language.
> 100 100 > 1/3 0.3333333333333333
There are functions that can help you convert strings to numbers
> parseInt("12345") 12345 > parseFloat("1.2345") 1.2345 > parseInt("1.2345") 1
The boolean type can have values of true or false. false, 0, empty strings, NaN, null, and undefined all evaluate to false and all other values evaluate to true.
Strings are sequences of Unicode characters that can be wrapped in single or double quote characters. The string type has built-in properties such as "length" to give you access to attributes about the string
> var s = "hi there" > s 'hi there' > s.length 8
JavaScript resolves around objects and are something you will use in virtually any JavaScript-based coding effort. They are extensible allowing you to add and remove properties and methods. To create an Object, you can use the new keyword or use the preferred object literal syntax
> var o = new Object(); undefined > var o = {}; undefined
One of the benefits of Object-Literal syntax is that you can specify the contents of an object at initialization time
> var person = { first_name: "Joe", last_name: "Pruitt" };
You can also add a new property to an object like this
> person.weight = 180; 180 > person['weight'] = 180; 180 > user { first_name: 'Joe', last_name: 'Pruitt', weight: 180 }
To delete an object or property, you can use the keyword:
> delete user.weight true > user { first_name: 'Joe', last_name: 'Pruitt' }
The array type is a special implementation of the object type. To create arrays, you can use the traditional notation, or the array-literal syntax
> var a1 = new Array(); undefined > a1 [] > var a2 = []; undefined > a2 []
To add an item to an array, you can do one of the following
> var a = [] undefined > a.push("hi") 1 > a [ 'hi' ] > a[a.length] = "there"; 'there' > a [ 'hi', 'there' ]
JavaScript is a functional programming language in that functions are fully typed objects and can be extended, manipulated, and used as data.
A simple function is defined with the function keyword
> function hi(name) { console.log("Hey " + name + "!"); } > hi("Joe"); Hey Joe!
Parameters are declared in the parameter list after the function name. If too few parameters are passed in, the missing ones will be undefined. If too many parameters are passed in, then the extras are ignored.
You can create an anonymous function without a name by using the following syntax
> var hi = function(name) { console.log("Hey " + name + "!"); } > hi("Joe"); Hey Joe!
It is recommended to use named functions whenever possible as debugging becomes more difficult without a named context in exception chains.
Node.js has a couple of global variables that can come in handy.
It's beyond the scope of this article to get into all of the aspects of the JavaScript programming language. For more details into the concepts I've covered, as well as others in the language, check out the JavaScript reference on w3schools.com - http://www.w3schools.com/jsref/default.asp.