This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const str = 'zanè'; | |
const buf = Buffer.from(str); | |
console.log('String data', str); | |
console.log('String Length', str.length); | |
console.log('Buffer data', buf); | |
console.log('Buffer Length', buf.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const originalBuff = Buffer.from('Node is Best'); | |
console.log('originalBuff Before', originalBuff.toString()); | |
const slicedBuff = originalBuff.slice(0, 4); | |
slicedBuff[0]++; | |
console.log('originalBuff After', originalBuff.toString()); | |
console.log('slicedBuff', slicedBuff.toString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sum = (a, b) => { | |
return a + b; | |
}; | |
const result = sum(2, 3) | |
console.log(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
const sum = (a, b) => { | |
return a + b; | |
}; | |
const result = sum(2, 3) | |
console.log(result) | |
})() | |
sum(5, 8) // ReferenceError: sum not defined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const exportObj = {}; | |
(function () { | |
const sum = (a, b) => { | |
return a + b; | |
}; | |
const result1 = sum(2, 3) | |
console.log(result1) //5 | |
exportObj.sum = sum; | |
})() | |
const result2 = exportObj.sum(5, 8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (exports, require, module, __filename, __dirname) { | |
// Module code actually lives in here | |
const sum = (a, b) => { | |
return a + b; | |
}; | |
const result = sum(2, 3) | |
console.log(result) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log('Arguments given by node', arguments) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sum = (a, b) => { | |
return a + b; | |
}; | |
const multiply = (a, b) => { | |
return a * b | |
}; | |
exports.multiply = multiply; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports = {} // don't do this | |
const obj = { | |
firstName: 'Udit', | |
lastName: 'Tyagi' | |
} | |
// don't export items like this, as assigning will change the reference | |
exports = obj | |
//To correctly export, here we are not changing reference of exports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const os = require('os'); //node's core module | |
const express = require('express') // third party module | |
const operations = require('./operations.js'); //local module | |
//Do something with these modules | |
const result1 = operations.multiply(2, 4); | |
console.log('Multiply Result: ', result1)// 8 | |
const result2 = operations.sum(2, 3);// Error, as it is not exported. | |
console.log('Sum Result: ', result2) |
OlderNewer