Node.js supports JavaScript. So, JavaScript syntax on Node.js is similar to the browser’s JavaScript syntax.
Primitive Types
Below primitive types are supported in Node.js
- String
- Number
- Boolean
- Undefined
- Null
- RegExp
Note – Everything else is an object in Node.js
Loose Typing
Loose Typing means variables are declared with any type associated with the declaration. JavaScript is intelligent enough to find out the proper type for the declaration. For example – in the blow code, I have declared two variables – name and loveNode, now based the value associated with the variables, JavaScript make the first one as String variable and second one as Boolean variable.
var name = 'Sudipta Deb';
var loveNode = true;
Objects
Object literal are treated the same as browser’s JavaScript. The below code, I have an object name with two properties firstName and lastName.
var name = {
firstName: 'Sudipta',
lastName: 'Deb'
}
Functions
Functions are first class citizens in Node’s JavaScript, similar to the browser’s JavaScript. A function can have attributes and properties also. It can be treated like a class in JavaScript.
function printMyName(){
console.log('Hello World');
}
printMyName();
Buffer
Node.js includes an additional data type called Buffer. Buffer is mainly used to store binary data, while reading from a file or receiving packets over the network.
Process object
Each Node.js script runs in a process. It includes process object to get all the information about the current process of Node.js application.
I will talk about the process object in future posts.
Defaults to local
Node’s JavaScript is different from browser’s JavaScript when it comes to global scope. In the browser’s JavaScript, variables declared without var keyword become global. In Node.js, everything becomes local by default.
Access Global Scope
In a browser, global scope is the window object. In Node.js, global object represents the global scope.
To add something in global scope, you need to export it using export or module.export. The same way, import modules/object using require() function to access it from the global scope.
0 Comments