Sunday, 25 June 2017
Install and use MongoDB in Windows
06:10
-
No comments
In this post I will share you how to install and use MongoDB in Windows in easy steps.
Step 1 : Download MongoDB
Go to MongoDB's official site and select the suitable version for your system and download.
Here is the link to download : https://www.mongodb.com/download-center?jmp=nav#community
Step 2 : Install it
To install MongoDB go to download location on your computer, double click the file and press Run when pop-up appears.
Step 3 : Add MongoDB to System Path Variable
Go to control panel then “Edit the system environment variables". when “System Properties” window appears, click on “Environment Variables…” and click on "New.." which appears in the bottom.
Give Variable name as "Path" and Variable value as "C:\Program Files\MongoDB\Server\3.4\bin" (navigate to MongoDB's bin folder to get the right path based on your system and folder location). Variable value depends on where you MongoDB script folder resides.
Step 4 : Run MongoDB server
Open a command prompt window and run the "mongod" command, this command run the "MongoDB" server at "27017" port and ready to connection with a client.
Step 5 : Run MongoDB client
Open another command prompt window and run the "mongo" command, this command create a client and establish the connection to server running at port "27017".
Step 6 : Create a new database
use studentDetails
Step 7 : Insert Data into database
db.students.insert([{"studentName":"Ankur","marks":"20"},
{"studentName":"Raj","marks":"30"}])
Step 7 : Check inserted Data into database
db.students.find().pretty();
I think today you learnt enough about mongoDB, Now go and give a try.
Happy Learning :-)
Step 1 : Download MongoDB
Go to MongoDB's official site and select the suitable version for your system and download.
Here is the link to download : https://www.mongodb.com/download-center?jmp=nav#community
Step 2 : Install it
To install MongoDB go to download location on your computer, double click the file and press Run when pop-up appears.
Step 3 : Add MongoDB to System Path Variable
Go to control panel then “Edit the system environment variables". when “System Properties” window appears, click on “Environment Variables…” and click on "New.." which appears in the bottom.
Give Variable name as "Path" and Variable value as "C:\Program Files\MongoDB\Server\3.4\bin" (navigate to MongoDB's bin folder to get the right path based on your system and folder location). Variable value depends on where you MongoDB script folder resides.
Step 4 : Run MongoDB server
Open a command prompt window and run the "mongod" command, this command run the "MongoDB" server at "27017" port and ready to connection with a client.
Step 5 : Run MongoDB client
Open another command prompt window and run the "mongo" command, this command create a client and establish the connection to server running at port "27017".
Step 6 : Create a new database
use studentDetails
Step 7 : Insert Data into database
db.students.insert([{"studentName":"Ankur","marks":"20"},
{"studentName":"Raj","marks":"30"}])
Step 7 : Check inserted Data into database
db.students.find().pretty();
Happy Learning :-)
Saturday, 10 June 2017

Javascript interview questions: typeof
13:35
-
2 comments
Question : what is typeof in javascript
Answer : The typeof operator returns a string indicating the type of the unevaluated operand.
Example : var a = 1;
console.log(typeof a);// number
Question : Write the output
console.log(typeof Infinity);
Answer : number
Question : Write the output
console.log(typeof null);
Answer : 'object';
Question : Write the output
var a = [1 ,2 ,3];
console.log(typeof a);
Answer : 'object';
Question : Write the output
console.log(typeof String('abc'));
Answer : 'string';
Question : Write the output
console.log(new String('abc'));
Answer : 'object';
Question : Write the output
typeof g;
Answer : "undefined";
Question : Write the output
console.log(typeof NaN );
Answer : "number";
* In ES6 use Number.isNaN() function for checking the value. Otherwise you can also use isNaN() .
Answer : The typeof operator returns a string indicating the type of the unevaluated operand.
Example : var a = 1;
console.log(typeof a);// number
Question : Write the output
console.log(typeof Infinity);
Answer : number
Question : Write the output
console.log(typeof null);
Answer : 'object';
Question : Write the output
var a = [1 ,2 ,3];
console.log(typeof a);
Answer : 'object';
Question : Write the output
console.log(typeof String('abc'));
Answer : 'string';
Question : Write the output
console.log(new String('abc'));
Answer : 'object';
Question : Write the output
typeof g;
Answer : "undefined";
Question : Write the output
console.log(typeof NaN );
Answer : "number";
* In ES6 use Number.isNaN() function for checking the value. Otherwise you can also use isNaN() .

Javascript interview questions: Hoisting , delete , use strict
12:56
-
1 comment
Question Hoisting in Javascript.
Answer: Hoisting is JavaScript's default behavior of moving declarations to the top.
Question: Write the output
x = 5;
console.log(x);
var x;
Answer: 5;
Question: Write the output
var x;
x = 5;
console.log(x);
Answer: 5;
Question: Write the output
"use strict";
console.log(x);
var x;
x = 5;
Answer: undefined;
Question: Write the output
"use strict";
console.log(x);
var x = 5;
Answer: undefined;
Question: Write the output
"use strict";
console.log(x);
x = 5;
Answer: Uncaught ReferenceError: x is not define;
Question. "use strict" in Javascript.
Answer: The purpose of "use strict" is to indicate that the code should be executed in "strict mode".
Syntax : "use strict";
Question: Write the output
"use strict";
x = 5;
console.log(x);
Answer: ReferenceError: x is not defined
Question: Write the output
x = 5;
console.log(x);
Answer: 5
* x will be create as a global varible
Question: Write the output
"use strict";
console.log(x);
x = 5;
Answer: ReferenceError: x is not define;
Question: Write the output
"use strict";
var x = y = 2;
Answer: ReferenceError: y is not defined;
Question. delete in Javascript.
Answer: it will delete object properties.The delete operator will not delete ordinary variables.However, it will delete "global variables," since they are actually properties of the global object.if in strict mode it will throw an error.
Question: Write the output
"use strict";
var x = 2;
delete x;
console.log(x);
Answer: SyntaxError: Delete of an unqualified identifier in strict mode.
Question: Write the output
var x = 2;
delete x;
console.log(x);
Answer: 2
Question: Write the output
x = 2;
delete x;
console.log(x);
Answer: undefined
Question: Write the output
var t = "test";
var test2.t2 = t;
delete test2.t2;
console.log(t);
Answer : "test";
Answer: Hoisting is JavaScript's default behavior of moving declarations to the top.
Question: Write the output
x = 5;
console.log(x);
var x;
Answer: 5;
Question: Write the output
var x;
x = 5;
console.log(x);
Answer: 5;
Question: Write the output
"use strict";
console.log(x);
var x;
x = 5;
Answer: undefined;
Question: Write the output
"use strict";
console.log(x);
var x = 5;
Answer: undefined;
Question: Write the output
"use strict";
console.log(x);
x = 5;
Answer: Uncaught ReferenceError: x is not define;
Question. "use strict" in Javascript.
Answer: The purpose of "use strict" is to indicate that the code should be executed in "strict mode".
Syntax : "use strict";
Question: Write the output
"use strict";
x = 5;
console.log(x);
Answer: ReferenceError: x is not defined
Question: Write the output
x = 5;
console.log(x);
Answer: 5
* x will be create as a global varible
Question: Write the output
"use strict";
console.log(x);
x = 5;
Answer: ReferenceError: x is not define;
Question: Write the output
"use strict";
var x = y = 2;
Answer: ReferenceError: y is not defined;
Question. delete in Javascript.
Answer: it will delete object properties.The delete operator will not delete ordinary variables.However, it will delete "global variables," since they are actually properties of the global object.if in strict mode it will throw an error.
Question: Write the output
"use strict";
var x = 2;
delete x;
console.log(x);
Answer: SyntaxError: Delete of an unqualified identifier in strict mode.
Question: Write the output
var x = 2;
delete x;
console.log(x);
Answer: 2
Question: Write the output
x = 2;
delete x;
console.log(x);
Answer: undefined
Question: Write the output
var t = "test";
var test2.t2 = t;
delete test2.t2;
console.log(t);
Answer : "test";
Saturday, 25 March 2017

Classes in JavaScript
10:40
-
No comments
Classes are the well-known concept in object oriented programming. Programmer those working on C++, Java etc are aware of what is Class and what we can achieve using Classes.With ECMAScript 2015 version JavaScript also introduced Classes.
According to MDN -
"JavaScript classes introduced in ECMAScript 2015 are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance."
let's go bit deeper and learn how Classes works in JavaScript.
Class declarations: Use the class keyword and give class name.
According to MDN -
"JavaScript classes introduced in ECMAScript 2015 are syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax is not introducing a new object-oriented inheritance model to JavaScript. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance."
let's go bit deeper and learn how Classes works in JavaScript.
- JavaScript classes introduced in ECMAScript 2015.
- To declare a class, you use the class keyword with the name of the class.
- JavaScript does not have classes, the way that Java and other languages have classes.
- JavaScript's class is (mostly) just syntactical sugar for prototypes, which are very different from traditional classes.
- A distinctive feature of classes is the function called the constructor. This is where you initialize your object's properties.
- You don't have to define a constructor function. If you choose not to, the engine will insert an empty one for you.
- In JavaScript, there is two way to define the class.
Class declarations: Use the class keyword and give class name.
class Rectangle{
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Class expressions: class expression can be named or unnamed in JavaScript.
// unnamed
var Rectangle = class {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
// named
var Rectangle = class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
};
An important difference between function declarations and class declarations is that function declarations are hoisted and class declarations are not.
//Example
var p = new Rectangle(); // ReferenceError
class Rectangle {}
The constructor method is a special method for creating and initializing an object created with a class. A constructor can use the super keyword to call the constructor of a parent class.
Prototype methods:
Here is the example to create prototype method in JavaScript
class Rectangle {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
const square = new Rectangle(10, 10);
console.log(square.area);
Static methods:
- The static keyword defines a static method for a class.
- Static methods are called without instantiating their class and cannot be called through a class instance.
- Static methods are often used to create utility functions for an application.Below is the example to create static method in JavaScript.
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx*dx + dy*dy);
}
}
const p1 = new Point(5, 5);
const p2 = new Point(10, 10);
console.log(Point.distance(p1, p2));
ES6 is finalized, but not fully supported by all browsers (e.g., ES6 Firefox support). To use ES6 today, get a compiler like Babel.For more information and examples on ES6 classes, take a look at this link MDN:CLASSES.
Happy Learning :-)
Sunday, 19 March 2017

JavaScript ES6 : Difference between var, let and const
03:26
-
No comments
In this tutorial I am going to write about variable declaration in JavaScript.So lets start with some basic difference between var, let and const.
var
- var is scoped to the nearest function block.
- You can declare the same variable multiple times using var.
- You can use variable before even initialize.
- Global variables defined with var will be added as properties on the global window object.
//Example
function test(){
for( var i = 0; i < 5; i++ ) {
//i is visible here
}
//i is visible here
}
//i is not visible here
// You can declare the same variable multiple times using var.
'use strict';
var a;
var a;
//You can use variable before even initialize.
function fun () {
typeof a;
var a = 'big';
}
fun();
/*Global variables defined with var will be added as properties
on the global window object*/
var a = 'go'; // globally scoped
console.log(window.a); // 'go'
let
- let is scoped to the nearest enclosing block (both are global if outside any block), which can be smaller than a function block.
- let says variable will be used only in the block it’s defined in.
- The use case for `let` tends to be for loops or mathematical algorithms.
- Use let where you need to reassign a variable like in loops.
- Assuming strict mode,you can't declare the same variable multiple times using let.
- Always initialize your identifiers before you try to use them.
- Global variables defined with let will not be added as properties on the global window object
for( let i = 0; i < 5; i++ ) {
//i is only visible in here
//and there is a separate i variable for each iteration of the loop
}
//i is not visible here
// You can't declare the same variable multiple times using let.
'use strict';
let a;
let a; // SyntaxError: Identifier 'b' has already been declared
//always initialize your identifiers before you try to use them
function fun () {
typeof a;
let a = 'big';
}
fun(); // Uncaught ReferenceError: bar is not defined
/*Global variables defined with let will not be added as properties
on the global window object*/
let a = 'go'; // globally scoped
console.log(window.a); // undefined
const
- Constants are block-scoped
- The value of a constant cannot change through re-assignment, and it can't be redeclared.
- const says the identifier won’t be reassigned.
- Naming convention for const variable is to use all-uppercase letters.
//Example
// define MY_NUM as a constant and give it the value 17
const MY_NUM = 17;
// this will throw an error
MY_NUM = 20;
// will print 17
console.log('my number is: ' + MY_NUM);
// trying to redeclare a constant throws an error
const MY_NUM = 20;
// the name MY_NUM is reserved for constant above, so this will also fail
var MY_NUM = 20;
// this throws an error also
let MY_NUM = 20;
I feel you found this article useful and now you got a clear picture about where and how to use var, let and const more effectively.
Happy Learning :-)
Saturday, 18 March 2017

JavaScript : Data types and operation between data.
13:19
-
No comments
JavaScript is the language, which is becoming the universal programming language for the web and currently, it is not only popular for front-end but back-end as well.
When starting with JavaScript people generally get confused with JavaScript data types.In this tutorial, I am going to share how JavaScript data type works.
These are the few key points to mention
- JavaScript is a loosely typed or a dynamic language.
- You don't have to declare the type of a variable ahead of time, it will get determined automatically while the program is being processed.
Data types
The latest ECMAScript standard defines seven data types:
Six data types that are primitives, primitives are immutable values (values, which are incapable of being changed):
- Boolean
- Null
- Undefined
- Number
- String
- Symbol (new in ECMAScript 6)
See the example
Var test = "Mark"; // test is a String
Var test = 123; // test is a Number
Var test = true; // test is a Boolean
Here you can see, I reassigned variable with different type and based on the given value JavaScript will dynamically identify what will the type of the data.Until this stage, everything is perfectly fine but you start getting into the trouble when JavaScript decides the type of the variable based on how that variable is used.
Let see the example:
'43' - 3 // 40
3 - '43' //-40
'4' - '6'//-2
'er' - 6 // NaN
6 - 'er' // NaN
53 + 'y'// "53y"
'y' + 53 // "y53"
'4' + '6'// "46"
'43' + 3 // "433"
3 + '43' // "343"
3 + 43 // 46
'6' * 3 // 18
'6' * 'b' //NaN
6 * 'b' //NaN
One way to fix this problem is to be explicit about the variables.check the example
// previous code
'43' + 3 // "433"
// explicit
var num = "43";
console.log(parseInt(num, 10) + 3); // 46
In this example, I am using parseInt which is saying to the JS, whatever in the num variable parse that as the integer before performing any operation.This is the one way to get rid of such kind of problem where JavaScript dynamically decides type of data.
Now I feel you understood some basics of the concept, so it's time to give it try.To find more you can check here.
JavaScript data types and data structures
Happy Learning :-)
Subscribe to:
Comments (Atom)
