CyrefJS
CCyrefJS
فا
Back to documentation
Function/Since 1.38.0

identity

Returns the given value unchanged.

identity(value):T

Parameters

valueTrequired

The value to return.

Returns

T

Returns the exact value passed to the function.

Examples

01

Basic usage

Returns the provided number unchanged.

TypeScript
const result = identity(42);

console.log(result);
// 42
02

With strings

The function preserves the string value and its type.

TypeScript
const result = identity("Hello");

console.log(result);
// "Hello"
03

With objects

Objects are returned as the same value passed to the function.

TypeScript
const user = {
  name: "Amir",
  age: 20,
};

const result = identity(user);

console.log(result);
// { name: "Amir", age: 20 }
04

Generic type inference

TypeScript automatically infers T from the provided value.

TypeScript
const numberValue = identity(123);
// T is inferred as number

const stringValue = identity("CyrefJS");
// T is inferred as string

const booleanValue = identity(true);
// T is inferred as boolean
05

Explicit generic type

You can explicitly provide the generic type when needed.

TypeScript
const result = identity<number>(42);

console.log(result);
// 42
Built with@cyref/js·Official documentation