# Evaluate Postfix Expressions Using Stack In Javascript

Category : TECH Author : Ankur Rastogi Date : Fri Jun 03 2016 Views : 251

Postfix expressions are easy to evaluate in comparison to infix expressions.

#### What is Infix notation?

The expression of the form of **a op b**, where operator is in between of bot the operands.

#### What is Postfix notation?

The expression of the form of **a b op**, where both the operands are followed by operator.

We have told Infix To Postfix Conversion Using Stack in Javascript in previous article. Postfix expressions don't have brackets and thus they are easy to evaluate. Here in this section we will tell you how to evaluate postfix expressions. Now let us see how to evaluate postfix expressions

### Algo

/**

* * Algo

*

* Create a stack to push ond pop the operands

*

* Run a loop for strings length

* Now look for each element in the string

* If element is operand than push it to stack

* If element is operator than pop two elements from the stack, apply operator on them and push the result back to stack

* Once the string is traversed completely

* There will be only one operand left in stack and that is our answer otherwise it is an improper string

*

*/

We will use stack to solve this problem. Please go through this Stack Basic Implementation in Javascript to learn about stacks.

### Implementation

"use strict";

**//Requireing Stack class**

var Stack = require("../");

**//To check whether element is operand or not**

function isOperand(ch){

return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') || (ch >= '1' && ch <= 9));

}

**//Function to evaluate Postfix**

function evaluatePostFix(postfixStr, stack){

var count = 0;

**//Loop while count is less than postfix string length**

while(count < postfixStr.length){

**//check whether element is operand or not**

if(isOperand(postfixStr[count])){

**//push element to stack**

stack.push(postfixStr[count]);

} else {

if(stack.size() < 2){

console.log("Improper postfix");

break;

}

**//Pop top two elements from stack**

var operand2 = stack.pop();

var operand1 = stack.pop();

**//Evaluate operand1 operator and operand2**

operand1 = eval(operand1 + postfixStr[count] + operand2);

console.log(operand1);

**//Push result operand1 back to stack**

stack.push(operand1);

}

**//Increment the counter**

count ++;

}

**//Last element is the result of the evalution pop it and return to give result.**

return stack.pop();

}

**// InfixToPostfix Class Creation**

var InfixToPostfix = function(){

}

**//Method to evaluate postfix**

InfixToPostfix.prototype.evaluatePostFix = function(postfix){

var stack = new Stack();

return evaluatePostFix(postfix, stack);

}

module.exports = InfixToPostfix;

Source code for this article can be found @ Evaluate Postfix Expressions Using Stack In Javascript

### Test Case

We are using mocha test cases. Please run mocha <filename> to run the test case.

"use strict";

var InfixToPostfix = require("../Stack/InfixToPostfix");

var infixToPostfix = new InfixToPostfix();

describe("#Stack Practise Questions", function(){

it("#Stack Evaluate Postfix", function(done){

console.log(infixToPostfix.evaluatePostFix("42^3*3-84/11+/+"));

done();

});

});

Postfix expressions are easy to evaluate in comparison to infix expressions.

#### What is Infix notation?

The expression of the form of **a op b**, where operator is in between of bot the operands.

#### What is Postfix notation?

The expression of the form of **a b op**, where both the operands are followed by operator.

We have told Infix To Postfix Conversion Using Stack in Javascript in previous article. Postfix expressions don't have brackets and thus they are easy to evaluate. Here in this section we will tell you how to evaluate postfix expressions. Now let us see how to evaluate postfix expressions

### Algo

/**

* * Algo

*

* Create a stack to push ond pop the operands

*

* Run a loop for strings length

* Now look for each element in the string

* If element is operand than push it to stack

* If element is operator than pop two elements from the stack, apply operator on them and push the result back to stack

* Once the string is traversed completely

* There will be only one operand left in stack and that is our answer otherwise it is an improper string

*

*/

We will use stack to solve this problem. Please go through this Stack Basic Implementation in Javascript to learn about stacks.

### Implementation

"use strict";

**//Requireing Stack class**

var Stack = require("../");

**//To check whether element is operand or not**

function isOperand(ch){

return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <='Z') || (ch >= '1' && ch <= 9));

}

**//Function to evaluate Postfix**

function evaluatePostFix(postfixStr, stack){

var count = 0;

**//Loop while count is less than postfix string length**

while(count < postfixStr.length){

**//check whether element is operand or not**

if(isOperand(postfixStr[count])){

**//push element to stack**

stack.push(postfixStr[count]);

} else {

if(stack.size() < 2){

console.log("Improper postfix");

break;

}

**//Pop top two elements from stack**

var operand2 = stack.pop();

var operand1 = stack.pop();

**//Evaluate operand1 operator and operand2**

operand1 = eval(operand1 + postfixStr[count] + operand2);

console.log(operand1);

**//Push result operand1 back to stack**

stack.push(operand1);

}

**//Increment the counter**

count ++;

}

**//Last element is the result of the evalution pop it and return to give result.**

return stack.pop();

}

**// InfixToPostfix Class Creation**

var InfixToPostfix = function(){

}

**//Method to evaluate postfix**

InfixToPostfix.prototype.evaluatePostFix = function(postfix){

var stack = new Stack();

return evaluatePostFix(postfix, stack);

}

module.exports = InfixToPostfix;

Source code for this article can be found @ Evaluate Postfix Expressions Using Stack In Javascript

### Test Case

We are using mocha test cases. Please run mocha <filename> to run the test case.

"use strict";

var InfixToPostfix = require("../Stack/InfixToPostfix");

var infixToPostfix = new InfixToPostfix();

describe("#Stack Practise Questions", function(){

it("#Stack Evaluate Postfix", function(done){

console.log(infixToPostfix.evaluatePostFix("42^3*3-84/11+/+"));

done();

});

});

**Disclaimer:** The above content reflect author’s personal views and do not reflect the views of OYEWIKI. Neither OYEWIKI nor any person/organization acting on its behalf is liable to accept any legal liability/responsibility for any error/mislead in this information or any information available on the website. This website in no way accepts the responsibility for any loss, injury, damage, discomfort or inconvenience caused as a result of reliance on any information provided on this website.

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.