Skip to content

Sign of a number

TypeScript if / else if + types

Same task in: JavaRust

Flowchart (ISO 5807)

YesNoYesNoStartInput nn > 0Return 1Endn < 0Return −1EndReturn 0EndFigure 1 — sign

Source code

TypeScript
function sign(n: number): number {
    if (n > 0) return 1;
    else if (n < 0) return -1;
    else return 0;
}

The editor opens with this code — change it and the chart redraws.

Language
TypeScript
Constructs
if / else if + types
Topic
Branching
Formats
Word, Visio, draw.io, PDF, SVG

Related examples