Defining and Accessing Variables

  var x = y = 5;   var z = 10;   var results = "x + y + z = " + (x+y+z);   var s1 = "jQuery";   var s2 = "JavaScript"   var s3 = s1 + " & " + s2;

JavaScript makes it easy to define variables. Variables are assigned using var name = value; syntax. The var keyword tells JavaScript that this is a new variable name. Once the variable has been assigned a value, you can access it using the variable name. You can define multiple variables on a single line using var name1 = name2 = value; syntax.

When you assign a variable to an expression, such as x + y + z, the expression is evaluated before assigning the variable a value. The following code snippet shows you how to define and access ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.