Jump to content

Definition of a Literal and a variable


Thomas Garrod

Recommended Posts

I am struggling with the JavaScript tutorial. Can someone please help me nail down the definition of a literal? The tutorial tells us "The JavaScript syntax defines two types of values: Fixed values and variable values." It then informs us that "...fixed values are called literals and variable values are called variables." I can memorize that, but I'm not sure what it means. Does this means that a variable called age has a literal value? For example, when I fill a form asking for my first name, assuming it is linked to a database variable called FirstName, Thomas would be a literal? What if the variable is age, would the number entered by a fixed variable or literal, even though that number will change in a few months?

 

Is a variable value then the conceptual container that holds literals (and is a null value a literal)?

 

So, is fair to say that a literal can be numbers (with or without decimals) or strings?

 

 

Link to comment
Share on other sites

Literal is a value that's written right in the code.

There are literal values being assigned to variables:

var a = "Literal string";var b = 5; // Literal numbervar c = false; // Literal booleanvar d = ["a", "b", "c"]; // Literal array with literal strings in itvar e = { "a" : 1 }; // Literal object with a property called "a" with value 1

Here are examples of non-literal values being assigned to variables

var a = giveMeAString(); // The value is returned by the function and it not explicitly written in the codevar b = a; // "a" is a variable, it's not a literal valuevar c = Math.PI; // The value of PI is stored in the Math.PI property.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...