EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript get
 

TypeScript get

Updated April 5, 2023

TypeScript get

 

 

Introduction to TypeScript get

Whenever we need to access the property of any object in TypeScript, we make use of a method called the get method, using which the value of a variable can be extracted. The get method is used in the program using the keyword get, and the get method can be made public, private or protected. The get method used in the program can be called using the name of the class in which the get method is defined, or the get method can be called directly. The compiler should be set to EMCAScript 5 or higher to make use of get method in a program, and it is not supported below EMCAScript 5.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax to Declare get Method in TypeScript:

get name_of_the_method(){
//block of code
}

Where name_of_the_method represents the method name defined using the get keyword.

Working of get Method in TypeScript

  • The get method can be defined in a program to extract the value of any variable or to access the property of any object in TypeScript.
  • The get keyword is used in the program along with the name of the method within which the code to be executed on the object.methodname() is written.
  • The get method can be made public, private or protected, and the get method used in the program can be called using the name of the class in which the get method is defined, or the get method can be called directly.
  • By using the get method, we can achieve control over how an object can be accessed in a program.

Examples of TypeScript get

Given below are the examples of TypeScript get:

Example #1

TypeScript program to demonstrate the usage of get method to obtain the factorial of a given number and display it as the output on the screen.

Code:

class check
{
//a private variable of type number is defined and stored in value
private value:number = 10;
//a private variable of type number is defined and stored in u
private u:number;
//a private variable of type number is defined and stored in fact
private fact = 1;
//for loop is defined to compute the factorial by decrement the given value stepwise by 1 until it is greater than or equal to 1, inside the method factorial by using the keyword get
get factorial()
{
for(this.u = this.value;this.u>=1;this.u--)
{
this.fact = this.fact * this.u;
}
return this.fact;
}
}
//The factorial of the given value is displayed as the output on the screen
console.log("The factorial of the given value is:\n")
//an instance of the class check is created within which the get method is defined to display the output on the screen
console.log(new check().factorial);

Output:

TypeScript get 1

In the above program, a private variable of type number is defined and stored in value. Then another private variable of type number is defined and stored in u. Then another private variable of type number is defined and stored in fact. Then a for loop is defined to compute the factorial by decrement the given value stepwise by 1 until it is greater than or equal to 1, inside the method factorial by using the keyword get. Then an instance of the class is created within which the get method is defined to display the output on the screen. The output is shown in the snapshot above.

Example #2

TypeScript program to demonstrate the usage of get method to find the power of a number and display it as the output on the screen.

Code:

class check
{
//a private variable of type number is defined and stored in value
private value:number = 10;
//a private variable of type number is defined and stored in u
private u:number = 2;
//get keyword is used to define the method power to obtain the power of a given number
get power()
{
return Math.pow(this.value, this.u);
}
}
//The power of the given value is displayed as the output on the screen
console.log("The power of the given value is:\n")
//an instance of the class check is created within which the get method is defined to display the output on the screen
console.log(new check().power);

Output:

power of a number

In the above program, a private variable of type number is defined and stored in value. Then another private variable of type number is defined and stored in u. Then by using the get keyword, the power method is defined to compute the power of a given number. Then an instance of the class is created within which the get method is defined to display the output on the screen. The output is shown in the snapshot above.

Example #3

TypeScript program to demonstrate the usage of get method to find the square root of a number and display it as the output on the screen.

Code:

class check
{
//a private variable of type number is defined and stored in value
private value:number = 4;
//get keyword is used to define the method squareroot to find the square root of a given number
get squareroot()
{
return Math.sqrt(this.value);
}
}
//The square root of the given value is displayed as the output on the screen
console.log("The square root of the given value is:\n")
//an instance of the class check is created within which the get method is defined to display the output on the screen
console.log(new check().squareroot);

Output:

TypeScript get 3

In the above program, a private variable of type number is defined and stored in value. Then by using the get keyword, squareroot method is defined to compute the square root of a given number. Then an instance of the class is created within which the get method is defined to display the output on the screen. The output is shown in the snapshot above.

Recommended Articles

We hope that this EDUCBA information on “TypeScript get” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Array
  2. TypeScript Types
  3. TypeScript Versions
  4. TypeScript Operators

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW