Meanstack Mid Questions
Meanstack Mid Questions
Example:
Function Add(a,b)
Console.log(a+b);
Add(10,20);
3) Function expression:-
Example:
};
console.log(getRectArea(3, 4));
4) Anonymous function:-
Console.log(“hello jimmy”);
Example:
6) IIFEs:-
➔ An immediately invoked function expression (IIFEs)
➔ It is a function expressions that expression that is
defined an execute immediately after its creation
➔ It is wrapped in parentheses to make it an
expression and followed by an additional set of
parentheses to invoke it
➔ To create closures
➔ IIFEs is used to create private and public variable
and methods
Example:
<script>
(function(dt){
document.write(dt.to locate time string());
})(newdate());
</script>
7) Arrow function:-
➔ They are a short hand syntax for anonymous function
that was introduced in ECMAScript (ES-6)
➔ They are also sometimes referred as fat arrow
function or lambda short hand
➔ Arrow function are typically used when writing
concise code and when same code needs to be execute
only once or twice throughout a program
➔ They can be assigned to variables or passed as
parameters to other function
Example:
Let helloworld = () =>{
Console.log(“hello world”);
}
Alert (helloworld);
8) Generator function:-
➔ Generator or generator function is the new concept
introduced in ES-6
➔ Generator function are a special kind of function
that can be paused and resumed during execution
➔ It provie=des you a new way of eorking eith itrators
and functions
➔ They are defined using the function * keyword
➔ Generator don’t provide random access
Example:
<script>
Function * greet(name)
{
Yield “good morning”;
yield “hello how are you”;
}
Const msg = greet(“sree leela”)
Console.log(msg.next().value);
Console.log(msg.next().value);
</script>
9) Async function:-
➔ The keyword async before a function makes the function return a
promise
Example:
function myFunction() {
return Promise.resolve("Hello");
}
var fs = require('fs');
• Read files
• Create files
• Update files
• Delete files
• Rename files
• Read Files
• The fs.readFile() method is used to read files on your
computer.
• Assume we have the following HTML file (located in the
same folder as Node.js)
• Example:
<html>
<body>
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('demofile1.html', function(err, data) {
res.writeHead(200, {'Content-
Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
</body>
</html>
Create Files
The File System module has methods for creating new files:
• fs.appendFile()
• fs.open()
• fs.writeFile()
Example:
var fs = require('fs');
fs.appendFile('mynewfile1.txt', 'Hello
content!', function (err) {
if (err) throw err;
console.log('Saved!');
});
Update Files
The File System module has methods for updating files:
• fs.appendFile()
• fs.writeFile()
Example:
var fs = require('fs');
Delete Files
To delete a file with the File System module, use
the fs.unlink() method.
Example:
var fs = require('fs');
Rename Files
To rename a file with the File System module, use
the fs.rename() method.
Example:
var fs = require('fs');
Example:
Consider this example, an object that represents a note with
an id, title, and date
const note = {
id: 1,
title: 'My first note',
date: '01/01/1970',
}
Spread operator:-
➔ A JavaScript object is a collection of key value
pairs
➔ It is an non primitive data type that can contain
various data types
➔ The spread operator could not be used with object
when it was first introduced in ES6
➔ The spread operator was eventully extended to object
in ES2018
➔ We can use the spread operator (…) to unpack elements
of an array
➔ Cloning and merging the array is simple through the
spread operator
Example:
// Create an Array
const tools = ['hammer', 'screwdriver']
const otherTools = ['wrench', 'saw']
console.log(allTools)
Rest operator:-
➔ The syntax appears the same as spread (...) but has the
opposite effect. Instead of unpacking an array or object into
individual values, the rest syntax will create an array of an
indefinite number of arguments.
➔ the rest parameter is mostly used with destructuring
syntax to consolidate the remaining properties in a new
object
Example:
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const {age, ...rest} = user;
console.log(age, rest);
what is node.js and Create a web server in
Node.js?
node.js:
Web server:
const http=require("http");
const server=http.createServer((req,res)=>{
res.write("welcome to node js");
res.end();
})
server.listen("5500");
Asynchronous programming:-
➔ asynchronous programming provides opportunities for a
program to continue running other code while waiting for a
long-running task to complete.
➔ Also known as nonblocking code
➔ The feature that enables asynchronous programming in these
languages is referred to as a callback function.
➔ It defines the relationship between two or more objects that
interact with each other within the same environment or system,
but at different time phases or time instant, and also does not
affect each other such as offline learning.
➔ We can also understand it with a simple example of an email, as in
this, people respond as per their convenience; hence it is an
asynchronous event.
➔ The data is sent from one end to another in the form of a byte of
characters.
Callback:-
Example:
function getData(callback) {
setTimeout(() => {
callback(data);
}, 1000);
getData((data) => {
console.log(data); });
Promises:-
Example:
function getData() {
setTimeout(() => {
resolve(data);
}, 1000);
});
getData()
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(error);
});
Async/Await:
Example:
setTimeout(() => {
resolve(data);
}, 1000);
});
console.log(data);
main();