Javascript Variable Types and Scope

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-variable-types-scope/
PINTEREST
PINTEREST
INSTAGRAM

We hold data in variables. Variable is used as a container for our data. Think about holding a name.

Code:

  • var is shortcut for variable. It is not a must but makes code readble. We can ommit the var while writing code
  • name  is the name of the variable when we need to access the data we will use name
  • assigns “Poftut” to the name variable

Output:

Types

As developing applications there will be a lot of different data to work. From numbers to logic true,false. We need to use some types to define our data.

Say we develop a calculator. We want to add numbers we can write a code like below.

Number is a type for javascript and differently interpreted by Javascript engine. Javascript engine is the interpreter that exists in the browser to run Javascrip code.

Or expressing logic values like True, False is different from text values.

We will look details of this types next chapters.

Scope

Scope means a variable use able area. For example we can not use variables declared other than current file or we can not access a varibale defined outside of function

  • Local Variable is only accessable from its scope
  • Global Variable is access able from anywhere

Code:

Output:

As we see a function creates its own scope and variables created in this local scope can not be accessed from global scope

LEARN MORE  Linux Bash Define, Call Functions

Variable Names

Variable names can use different characters but there is some limitations.

  • We can not use break as variable name because it is a keyword for Javascript and used by Javascript. It will create confussion and error to use break. Here is Javascript keywords.
abstract

boolean

break

byte

case

catch

char

class

const

continue

debugger

default

delete

do

double

else

enum

export

extends

false

final

finally

float

for

function

goto

if

implements

import

in

instanceof

int

interface

long

native

new

null

package

private

protected

public

return

short

static

super

switch

synchronized

this

throw

throws

transient

true

try

typeof

var

void

volatile

while

with

  • Variable names can not start with numbers. Which means
    • 12name is not valid variable name
    • name12  is valid variable name
  • Javascript is case-sensitive language which is true for variable names. Myvar is different from MYVAR
RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/javascript-variable-types-scope/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)