---
title: "Discussion on \"Global, local variable scope with example\" | Hashnode"
description: "Discussion on \"Global, local variable scope with example\". var age = 30;\n\nfunction getAge () {\n  if ( age === undefined ) {\n    var age = 20;\n    return age;\n  } else {\n    return age;\n  }\n}\n\nconsole.log(getAge());\nOutput:\n\n20\nExecute above code here"
url: "https://hashnode.com/posts/global-local-variable-scope-with-example/637cb0bc8f9f236f36009dbc"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Global, local variable scope with example](https://harikrishna.hashnode.dev/global-local-variable-scope-with-example?utm_source=hashnode&utm_medium=feed)

var age = 30; function getAge () { if ( age === undefined ) { var age = 20; return age; } else { return age; } } console.log(getAge()); Output: 20 Execute above code here

Nov 22, 2022 · 1 min read · harikrishna.hashnode.dev  
Read the article

-   [#global-scope](https://hashnode.com/tag/global-scope)
-   [#local-scope](https://hashnode.com/tag/local-scope)

## Written by

[Hari Krishna Anem](https://hashnode.com/@hkrishna)

@hkrishna · Hyderabad, India

  
[View profile](https://hashnode.com/@hkrishna)

Full stack developer

Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
