Open In App

What is the difference between ‘String’ and ‘string’ in TypeScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

Unlike JavaScript, TypeScript uses static typing, i.e. it specifies what kind of data the variable will be able to hold. Since TypeScript is a superscript of JavaScript, it also holds a distinction between a string and String. The usage of a string object in JS (or TS for that matter) is very minimal. String objects have the feature of adding a property to an object. 

In general, the string(with a small ‘s’) denotes a primitive whereas String(with an uppercase ‘S’) denotes an object. JavaScript supports five types of primitives and string is one of them. You will get the idea of Variables and Datatypes in JavaScript from linked article.

Primitive string: The string(with a small ‘s’) indicates a primitive, primitives are values that hold no properties. String literals and a few strings returned from a string function call can be classified as a primitive(string). A primitive string can be converted to an object by using a wrapper.

Syntax: 

var test1 = "A TypeScript variable of type 'string'";

Object String: The object is an accumulation of different properties. An object can call numerous methods corresponding to it.

Syntax:  

var test2 = new String('another test');
  • Example: 

Javascript




<script>
let a = new String("An object !");
let b = "A literal ! haha";
console.log(typeof(a));
console.log(typeof(b));
</script>


Output: 

object 
string 

In the above code, we see the type of variable “a” is an object whereas “b” is a primitive (string). It shows that a string literal is primitive. The important thing to note here is that we are able to use a method for “b” even though it is not an object. This is because JavaScript changes a primitive to its object when a method is called through it. This happens for a very small amount of time just to execute the method and returns back to its primitive self. Then why question may arrive why we will need String object! 

Need of String Object: 
When we use the keyword new, TS creates a new object every time unlike when you use a primitive type, if you have the same value for the variables then they point to the same memory. Refer to the below example. 

  • Example 1: The below example will shows how a1 and b1 are shown to be equal since they are literals having the same value, whereas a2 and b2 are shown to be different because we used the keyword new which creates two different objects. 

Javascript




<script>
let a1 = "Hello World";
let b1 = "Hello World";
console.log(a1 != b1);
let a2 = new String("Hello World");
let b2 = new String("Hello World");
console.log(a2 != b2);
</script>


Output: 

false
true
  • Example 2: The use of eval differs from primitive string to object. Refer to the following example. In this example, we will see how string and String differ when using eval() method. The string primitive is directly evaluated that is 25 * 25 gives you 525 as the output whereas the object will give the output as itself since eval() returns the argument unchanged if it is not a primitive string. We can also evaluate the object by changing it to a primitive string using toString() method.

Javascript




<script>
var a3 = '21 * 25' ;
console.log(eval(a3));
var b3 = new String("1 + 1");
console.log(eval(b3));
console.log(eval(b3.toString()));
</script>


Output: 

525 
String { "1 + 1" }
2

As we have learned above the string object can hold properties. We can use String objects to hold an additional value in the properties. Even though this not commonly used, it is still a feature of JS. 

var primitive = 'hello';
var object = new String('HELLO');
primitive.prop = 'world';//Invalid
object.prop = 'WORLD';//Valid

Difference between string and String:

string primitive String object
The string primitives are used extensively. The String object are scarcely used.
The string primitives only hold the value. The String object have the ability to hold the property.
The string are immutable thus are thread safe. The String object is mutable.
The string primitive has no methods. The String object has methods.
Cannot create two different literals with the same value. You can create new objects with the keyword ‘new’.
It is a primitive data type. Wraps primitive data type to create an object.
Passed by value that is copy of primitive itself is passed. Passed by reference to the actual data.
When using eval() these are directly treated as source code. When using eval() these are treaded as a string.

 



Last Updated : 03 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads