Skip to content

Latest commit

 

History

History
197 lines (157 loc) · 6.12 KB

File metadata and controls

197 lines (157 loc) · 6.12 KB

Hi folks In this Readme.md we will study functions and buffers 🐬

Introducing Functions ✔️

A function is a block of code which is used to perform a particular function. In node.js, they are fully typed objects that can be manipulated, extended, and passed around as data.

Structure of a function ->

function functionName() {
   // function body
   // optional return; 
}

| Note: All functions return a value. In case a return value is not mentioned then the function returns undefined.

For example-

function function1() { 
   return "hello"; 
} 
console.log(function1()); // hello

function function2() { 
} 
console.log(function2()); // undefined 

The output of the above code is 👀 ->

Parameterized Functions 🚩

To declare parameters for a function, simply list them in the parentheses.

For example-

function display(name) {
     console.log("parameter is " + name);
}
display();
display("CSS", "HTML", 4);

The output of the above code is 👀 ->

| Note: If too few parameters are passed into a function call, the resulting variables are assigned the value undefined. If too many are passed in, the extras are simply unused.

Functions Scope 📏

Every time a function is called, a new variable scope is created. Variables declared in the parent scope are available to that function. Variables declared within a function are not available outisde it.

For example-

var value= 25 ;

function printValue() {
     var value = 30 ;
     console.log(value);
 }

printValue();
console.log(value);

The output of the above code is 👀 ->

Why Buffers are necessary? ⌛

JavaScript is Unicode-friendly but does not know how to handle binary data. Node.js works with TCP streams and needs to read and write a file which uses binary streams of data. Using strings in these cases was the first approach by many developers but later it caused many problems because of which buffers were introduced.

Introducing Buffers 💻

Buffer class in Node.js is used to handle raw binary data. It corresponds to memory outside V8 heap. Buffer object is a global object. It does not need require directive to import modules.

For example -
The following code converts string "abcdef" into binary data and prints it to console -

var buf = Buffer.from('abcdef');
console.log(buf);

The output of the above code is 👀 ->

Let's Create Buffers ✈️

There are a few ways to create buffers.

Case 1 📌

Creating an empty buffer of length 20 ->

var buf = Buffer.alloc(20);
console.log(buf);
// this is print 20 bytes of zero

The output of the above code is 👀 ->

Case 2 📌

Creating an initialized buffer of length 10 ->

var buf = Buffer.from([ 8, 6, 7, 5, 3, 0, 9, 1]);
console.log(buf);
// this is print 8 bytes of values mentioned

The output of the above code is 👀 ->

Case 3 📌

Creating a buffer from a given string ->

var buf = Buffer.from("I'm a string!", "utf-8");
console.log(buf);
// this is print 8 bytes of values mentioned

The output of the above code is 👀 ->

Writing to Buffers ✏️

We use the following syntax to write to buffers ->

buf.write(string[, offset][, length][, encoding])

  • string − mentions string data to be written to buffer
  • offset − index of the buffer to start writing at (Default value is 0)
  • length − the number of bytes to write
  • encoding − Encoding to use. ('utf8' is the default encoding)

Code 📌

buf = Buffer.alloc(100);
len = buf.write("Simply Easy Learning");
console.log("Octets written : "+  len);

The output of the above code is 👀 ->

Reading from Buffers 📘

We use the following syntax to read from buffers ->

buf.toString([encoding][, start][, end])

- encoding − Encoding to use. ('utf8' is the default encoding)
- start − Beginning index to start reading (defaults to 0)
- end − End index to end reading (default is complete buffer)

Code 📌

buf = Buffer.alloc(26);
for (var i = 0 ; i < 26 ; i++) {
  buf[i] = i + 97;
}

console.log( buf.toString('ascii', 0, 7));

The output of the above code is 👀 ->

Below are some buffer methods ⭐


Method 👀 Description 👇
compare() compares two buffers
concat() concatenates one buffer object to another
copy() copies the speicfied number of bytes of a buffer
equals() compares two buffers and returns true if matched
slice() slices a buffer into a new buffer starting and ending at specified locations
toString() converts a buffer to string
toJSON() returns JSON of the buffer
values() returns an array of values of a buffer