EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript get
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript get

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.

Syntax to Declare get Method in TypeScript:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

get name_of_the_method(){
//block of code
}

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,950 ratings)

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

This is a guide to TypeScript get. Here we discuss the introduction, working of get method in TypeScript and examples, respectively. You may also have a look at the following articles to learn more –

  1. TypeScript Array
  2. TypeScript Types
  3. TypeScript Versions
  4. TypeScript Operators
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more