Meraki-Javascript-Syntax


Welcome to the Javascript Syntax Homepage, Guest!


Javascript | Syntax




Syntax is the set of exact rules for a specific language. Javascript syntax relates to how the programs are built, designed, or constructred. A program is generally an organized list of instructions the computer will read, and the computer will execute the instructions given to it by the programmer. The instructions given to the computer to be read are called statements. Javascript is considered a programming, but I am not sure if it is a Turing complete language or not. Javascript separates the statements by semicolons. The use of semicolons alone does not make Javascript a programming language, but we will dive deeper into that subject as the tutorial progresses. In HTML, Javascript programs are executed by the web browser, and could be called interpreted.


var x, y, z;
Three variables are created; x, y, and z. The variable statement ends in a semicolon.
x = 10;
y = 16;
z = 1985;

<!DOCTYPE html>
<html>
  <body>

    <h2>JavaScript Tutorials</h2>

    <p>The variables x, y, and z are assigned the values 10, 16, and 1985.</p>

    <p>Then a string is concatenated with the three variables to display James' birthday.</p>

    <p id="birthday"></p>            <!-- notice the null element -->

  <script>
      var x, y, z;
      x = 10;
      y = 16;
      z = 1985;

      result = "James' Birthday: " + x + "/" + y + "/" + z;

      document.getElementById("birthday").innerHTML = result;
  </script>

  </body>
</html>

The string variable result is a concatenated variable designed with x, y, and z in mind. The result variable is then interjected into a null element via the getElementById(id) method. This is a way to create dynamic functions within your website or web application to serve the information quicker. If the information is dynamically created and displayed, that means the information didn't need to be stored on the db, didn't need to be queried, didn't need to have countless function calls to modify the resulting information in a way you need, and it certainly didn't need to take 100s of lines of code to achieve what you can achieve with a few Javascript lines, right?




Javascript Statements encompass:

  • Values literals and variables
  • Operators arithmetic and assignment
  • Expressions computes an evaluation
  • Keywords actions to be taken
  • Comments leave a trace of what you were thinking



Values

can be defined within Javascript in two ways; a literal fixed value or a change variable value. A literal is a value that is fixed, or a value that does not change at all. Variables are named because they variably change, depending on the program, and you can not be certain what the value will be from time to time.

Literals

Numbers are one of the most common literals, and they can be written with or without decimals.

10.16

OR

1016

Strings are text that must be written inside of single or double quotation marks inside Javascript.

"James Patterson" or 'James Patterson'

Variables

the var keyword is used to declare variables in Javascript. The equals sign is used to assign the values to the variables The following code reads: a variable is created called month, and month is assigned the value of 10.

var month;

month = 10;

The value can then be inserted into a null element waiting for a data set, or replace a small bit of dynamic data.




Operators

can be used in multiple different ways, but mainly are used for arithmetic operations and assignment operations. Arithmetic operations are exactly what they seem to be; operations that involve arithmetic to a set value. Arithmetic operations include addition, subtraction, multiplication, division, and modulus to name a few. Javascript uses the assignment operator to assign a value to a variable. The equals sign is used during the assignment. The variable name, the equals sign, and then the values associated with the assignment.

Arithmetic

can be used within javascript quite easily. Javascript uses an extensive math library to perform any kind of calculation required of it.

<!DOCTYPE html>
<html>
  <body>

    <h2>JavaScript Arithmetic Operators</h2>

    <p id="tutorial"></p>

    <script>
        document.getElementById("tutorial").innerHTML = ((16 - 4 + ((2 + 4) * 6))/2);
    </script>

  </body>
</html>

Assignment

is when you create the variables and assign the variables values.

<!DOCTYPE html>
<html>
  <body>

    <h2>Assigning JavaScript Values</h2>

    <p>In JavaScript the = operator is used to assign values to variables.</p>

    <p id="tutorial"></p>

    <script>
    var x, y;
    x = 10;
    y = 16;
    document.getElementById("tutorial").innerHTML = x + "/" + y;
    </script>

  </body>
</html>




Expressions

are a combination of different values, variables, literals, operators, and more that might computer or document a value. The computation is known as an evaluation. Five times ten will always equal fifty. Expressions can use variables in their computation. The values can be different types; floats, doubles, integers, and strings.


"James" + " " + "Patterson" will evaulate to "James Patterson".

The expression will incorporate the evaluation of variables with numbers concatenated with strings. The code below will show you how to achieve this goal.

<!DOCTYPE html>
<html>
  <body>

    <h2>JavaScript Expressions</h2>

    <p>James Patterson has $<span id="tutorial"></span> in his pocket.</p>

    <script>
      var x;
      x = 5;
      document.getElementById("tutorial").innerHTML = x * 10;
    </script>

  </body>
</html>




Keywords

are used in Javascript to identify the actions to be performed within the HTML code. The var keyword informs the browser to create the variables. There will be more on keywords as the tutorials progress. The var keyword creates a variable or literal out of the programmers actions.


var x, y;
x = 10 + 5;
y = x + 15;
result = x + y;

The answer is not 30 if that is what you were thinking. Because x equals 15 when it is used in y to make y 30, but then result takes x, which is 15, and adds that to y which is 30. So, result will equal 45 when returned using Javascript.




Comments

are essential in any language, including Javascript, but creating effective comments is the goal one wants to achieve. Comments are used after two double slashes, //, or between two similar tags /* and */. Comments are always ignored within any programming language, and should be utilized to ensure reusability. Comments are great to catelog what you were thinking while programming the specific code. The comments will always be there, and will help you if you forget what is going on in the code.


Identifiers

are basically names, the names are used to name variables, keywords, functions, and labels to name a few. The rules for the legal names are similar to most other programming languages. Javascript must have the first character be a letter, underscore, or a dollar sign. The characters followed by one of the three can be letters, digits, underscores, or dollar signs. Numbers are not allowed to allow JS to easily determine numbers from identifiers.

Case Sensitive Identifiers

are identifiers where the case of the name completely matters. The variable names lastName and lastname are not the same two variables, but they are instead totally different variables. Javascript does not even interpret the words VAR and Var as the keyword var.

var lastname, lastName;
lastname = "Doe";
lastName = "Patterson";

Naming conventions

Javascript allows for naming conventions to be in the following ways:

  • Use Underscores first_name, last_name, ga_Tech
  • Upper Camel Case FirstName, LastName, GaTech
  • Lower Camel Case (my go to) firstName, lastName, gaTech
Javascript does use the Unicode character set, and that does cover the majority of the characters, punctuations, and symbols known throughout the world. Google the term Complete Unicode Reference for a larger scale understanding of the topic.




Next, we will take a look at

Just follow the buttons to continue the tutorial, Guest