Home / code / javascript

Javascript variable

Variables are a symbolic name for values in your application.
But there are a few rules.

The name must start with a letter or underscore ( _ ),
subsequent characters can also be digits(0-9) (digits can not be the first character)
Javascript is case sensitive so "name1" is not the same as "Name1".

You can declare a variable (giving a value) in 2 ways:

  • By assigning it a value. x = 42
  • With the keyword var. var x = 42

Javascript can understand the following type of values:

  • Numbers, like 42 or 3.544
  • Boolean values, (true or false)
  • Strings, like "word"
  • null, a keyword that gives a null value, primitive value.
  • undefined, a top-level property whose value is undefined, primitive value.

The nice thing about javascript is that it is a dynamically type language. Meaning you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution.

So you can add strings and numbers together:
x = "The answer is " + 42 //gives "The answer is 42"

Warning:
"37" - 8 //gives 29
but
"37" + 8 //gives 378

 

TOP

Latest script:

 

Books: