thadeuszlay
thadeuszlay

Reputation: 3015

Difference between interpolation and concatenation in JavaScript?

The language and library I'm using is JS and AngularJS. Am I right in assuming that "interpolation" and "concatenation" both means "combination of strings and variables"?

But "interpolation" is used when you're using the double curly brackets in AngularJS. And "concatenation" is used when you're using pure JS?

Or is there another diference that I missed?

EDIT: Is this interpolation or concatenation (I don't just put strings together but an a variable is resolved here)?

    var test1 = "Hello";
    var number = 2 + 3;
    console.log(test1 + " " + number);

Hmm.... I guess whenever I use AngularJS library to combine some expression, then it's interpolation, right?

Upvotes: 3

Views: 5259

Answers (3)

squiroid
squiroid

Reputation: 14037

In Angular js interpolation helps in getting model render on the view. Where as concatenation is to allow the strings to be join together with '+' it may or may not include interpolation .

Example:-

{{}} --> interpolation

It allows your model to bind in view:-

Model:-  $scope.name="rachit";

   View:-  <div>{{name}}</div>

   result in View after rendering :- <div>rachit</div>

'+'-->concatenation

It allows to concat the values either in view or in controller or even inside interpolation.

Controller:-
$scope.name="rachit"+"gulati";

View
<div>{{name+name}}</div> //Here interpolation also comes into picture.
result:-"<div>rachitgualtirachitgualti</div>"

UPDATE 1:-

EDIT: Is this interpolation or concatenation (I don't just put strings together but an a variable is resolved here)?

var test1 = "Hello";
var number = 2 + 3;
console.log(test1 + " " + number);

It is simple concatenation in javascript here angular is not come into picture.

UPDATE 2:-

Hmm.... I guess whenever I use AngularJS library to combine some expression, then it's interpolation, right?

Concatenation is general term for every programming to concat two strings together.Where as in angualr js interpolation is done by {{}} with the help of $interpolation service.

Upvotes: 5

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

Interpolation

Interpolation does mean that you want to get the code from the angular scope and show it on html page like suppose we have {{somevariable}} In this eg. it will evaluate the function in angular scope using $interpolation service.

Concatenation

It is used when you want to bind to two strings, It could be do it inside interpolation {{}} directive like {{someVariable + ' test'}} it will concate variable with test result would be someVariable test

Upvotes: 3

Chrillewoodz
Chrillewoodz

Reputation: 28338

Interpolation basically means that an expression gets resolved and concatenation is when you put a string together.

Upvotes: 0

Related Questions