Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit

JavaScript Writing Rules

JavaScript Writing Rules

It contains information about JavaScript spelling rules that should be considered when developing with JavaScript .

A computer program is instructions written to be processed by a computer.

There are different spelling rules for each programming language when writing the instructions.


JavaScript Syntax

Many of JavaScript's syntax are similar to JAVA programming, but JavaScript is also affected by Perl and Python. As a JavaScript character set Unicode Unicode (Universal Code) is an industry standard developed by the Unicode Consortium organization that assigns a numeric value to each character. character set is used. However, this does not mean that every Unicode encoding will be displayed. The image of the character called "Glyph" must also be defined in the document parser. Naturally, this possibility is valid for less used archaic languages. All characters of western languages ​​will be displayed without any problems. Character codes in other languages ​​continue to be added to parsers.


Supporting Unicode characters in JavaScript programming language means that every user-defined variable and object such as variable names and function names can be introduced and called with Turkish names in JavaScript programs. This increases the understandability of the program locally, but in international settings, the comprehensibility of programs written in local languages ​​is considerably reduced.


It ignores any whitespace, line spacing and tab characters, unless required by alphanumeric variables and regular expressions (RegExp) in the JavaScript language. Therefore, by making the most meaningful code arrangements for ourselves, we have the comfort of program writing. The JavaScript programming language exactly supports the Algol / Pascal / ADA free scripting form. Although the writing method is very flexible, it is very useful to stay within certain patterns stemming from habit. Because it will provide us great convenience for the future maintenance of the written code or code block.


In the JavaScript programming language, as in some programming languages, codes received in a block do not create a new scope. The JavaScript interpreter foresees that the notifications to be executed are enclosed in a notification block {} so that program elements such as if are not a single declaration, rather than a single declaration. The program steps to be used in the notification block are specific and the purpose of the notification block {} to be used in them is not to open a new scope to its content, but to ensure that the notifications with content are executed sequentially.


All variables in JavaScript begin with letters, underscores or (_ or sublink) or dollar ($) sign. Numbers and other characters are used after these three characters. Although variables can be initialized with the $ sign, in current usage this facility is only left to machine-generated automatic JavaScript codes. In other words, although there is no preventive rule in codes created by humans, the $ character is not used in variable names.


JavaScript is case sensitive . So if you create a variable with the same name, both uppercase and lowercase, JavaScript interprets these two variables separately. Sample Content, Content or ICERIK variables are different from each other and will be interpreted differently. You cannot create a variable with the following reserved keywords in JavaScript. Finally, there is no difference between single quotes and double quotes. It should be noted that if you start with single quotes, you should end with single quotes, and if you start with double quotes, you end your double quotes content.

The spelling rules are called syntax.
Javascript Syntax

The spelling rules are called syntax.

Code is the smallest piece of code that performs a certain action in JavaScript commands.

JavaScript codes end with a semicolon.

var abc;
a = 5;
b = 6;
c = a + b;

JavaScript codes consist of variables, operators, phrases, keywords, and comments.

Two different types of values ​​can be written, namely JavaScript fixed values and variable values .


Fixed values

Constant values ​​are values ​​written without being stored in any variable.

Fixed-value numbers are written without any quotation marks.

20.20
2020

Fixed-value texts are written in single or double quotes.


Variable values

In programming languages, variables are used to store data.

The var keyword is used to define a JavaScript variable .

The equal sign is used to assign a value to the defined variable.

In the example, x is defined as variable and then 2020 value is assigned to variable x .

var x;
x = 2020;

Variables, variables are a kind of data store where you can store any value and whose name we determine. Defining a variable is started using the var keyword let, followed by the name of the variable.

var  sirVariable;

After defining a variable, you can give it a value.

sirVariable =  'Sir Node' ;  

You can also do these operations in a single line.

var  sirVariable =  'Tom' ;  

You can call a variable by simply typing its name.

sirVariable;  

After giving a value to a variable before, you can change that value.

var  sirVariable =  'Tom' ;  
sirVariable =  'John' ; 

So why do we need variables? We need variables to do everything in programming. If we do not change the values, we cannot get a dynamic result. For example, we cannot produce a personal greeting or change the photo on display.

Strings in Javascript

String Values

A string variable consists of zero or more characters enclosed in double quotes (”) or single quotes ('). Whatever type of quotes a string value begins with, it must be ended with the same type of quotes. Therefore, quotes covering characters are enclosed in either double quotes or single quotes. In the example below, string values ​​are expressed.

"apple"  
'pear'  
"1234"  
"one line n other line"  
"Zafer's cat"

We can access and execute a string type variable or method and properties of a String type object by placing a period (.) At the end of the string variable . If your variable is a string type variable (primitive), JavaScript automatically converts this string type variable to a temporary String type object and calls the method and then this temporary String object is deleted from memory. You can also use the String.length property by appending the string type variables to the end.

console.log ( "Sir Node is the best" .length);   
// Prints the number of all symbols in the string, including the space.  
// Value written on the screen: 19

Integers in Javascript

Integers

Integers; They are represented in binary (base 2), octal (base 8), decimal (base 10) and hexadecimal (base 16).

  • Integer expressions preceded by 0b (or 0B) are binary. Binary integers only consist of 0 and 1 digits.
  • Integer expressions preceded by a 0 (zero) are octal. Octal integers only consist of digits (0-7).
  • A decimal integer value consists of numbers that do not start with a 0 (zero).
  • Integer expressions preceded by 0x (or 0X) are hexadecimal. Hexadecimal integers can contain the numbers 0-9, as well as letters between af ​​or AF.
1, 347 and -645 (decimal, base 10)  
047, 0504 and -017 (octal, base 8)   
0x1527, 0x05131 and -0xB1C7 (hexadecimal,  "hex"  or base 16)  
0b01, 0b1011 and -0b01 (binary, base 2) 

Decimal Values

A decimal value;

  • A decimal number that can be signed (can start with "+" or "-"),
  • A point ( " . " ),
  • Decimal (the other decimal number after the point),
  • A coefficient.

It may contain portions.

The coefficient part is an "e" or "E" character followed by an integer that can be signed. In order for the decimal value to be defined, it must contain at least one digit followed by a decimal number and coefficient "e" (or "E").

Syntax:

[(+ | -)] [numbers] [. numbers] [(E | e) [(+ | -)] numbers]
3.1415926  
-.123456789  
-3.1E + 12  
.1e-23 

Boolean in Javascript

Boolean Values

There are two kinds of values ​​in Boolean type. true (true) or false (false).

Do not confuse the Boolean (primitive) type values true and false with the Boolean type true and false values. It is a boolean type object, it was created by taking the data type of boolean type and adding some additional properties on it

Array in Javascript

Array Values

Array values ​​are variables in the form of a list with 0 (zero) or more expressions. There are square brackets [] at the beginning and end of the list that open / close the list. When you create an array and assign values ​​to this array, an array with the values ​​you specify as its element is generated and the length of the array is the number of values ​​you specify.

In the example below, drinks named array is created that contains 3 elements and is therefore 3 long :

var  coffees = [ "coffee" ,  "tea" ,  "vodka" ];  

If an array is created using a constant value in the top-level block, JavaScript interprets the array whenever it runs any statement containing that array. In addition, arrays defined in the function are created in memory as a new array at each function call.

Array values ​​are also Array objects.

You do not have to specify every value when defining an array. If you create an array by putting two commas consecutively, an undefined value is generated for each undefined value between commas .

var  fishes = [ "cuttlefish" ,  "bluefish" ]; 

This array has 2 elements with values ​​and one empty element.

(fishes [0] = "cuttlefish", fishes [1] = undefined, fishes [2] = "bluefish").

If you put commas at the end of the elements in the array, the comma will be ignored by JavaScript.

var  places = [ 'home' ,  'school',, ];  

In the above example the length of the array, places[1] and the places[3] values are not defined, but it still is 4. Only the last comma is ignored.

Understanding the behavior of extra commas is crucial to learning the JavaScript language, but when you write your own code undefined, specifying undefined elements in the array will significantly improve the readability and maintainability of your code.

Object in Javascript

Object Values

An object variable consists of a list of zero or more attribute names and values ​​associated with those attributes surrounded by braces {}. You should not create an object by just starting with curly braces without a variable name at the beginning of a line of code . If you do, the JavaScript interprets the {character at the object start as starting a block of code, resulting in either an error message, or the code you wrote does not work as expected.

Below is an example with the object value. carGallery The first element of the object defines a property named with the Fiatvalue " " arabam; the second element is the arabaAlproperty and function (araçTipleri("Honda")) contains the result of the function immediately after the function is executed; The third element is the sporArabaAlproperty, it hayalimdekiArabauses the already existing variable.

    var  dreamCar =  "Bugatti" ;  
  
    function  vehicleTypes (brandName) {  
      if  (brandName == " Wolksvagen  " ) {  
        return  brandName;  
      }  else  {  
        return "Sorry, unfortunately"  + brandName +  "brand car is not available." ;   
      }  
    }  
      
    var  carGalery = {   
        myCar:  "Fiat" ,   
        buyCar: carTypes ( " Wolksvagen " ),   
        buySportsCar: carTypes (my dream car)  
    };  
      
    console.log (carGalery.myCar);   // Fiat  
    console.log (carGalery.buyCar);  // Wolksvagen  
    console.log (carGalery.buySportsCar); // Sorry, unfortunately we do not have Bugatti cars.

In addition, a numeric or string can be used in place of a value, property name, or name of another nested object. We can see these options in the example below:

var  carGalery = {   
    cars: {s:  "Saab" ,  "j" :  "Jeep" },   
    7:  "Mazda"   
};
console.log (carGalery.cars.j); // Jeep  
console.log (carGalery [7]); // Mazda

Object property names can be any string expression, including an empty string. If the attribute name is not a valid JavaScript identifier, it must be written in quotes. In addition, invalid property names .cannot be accessed with periods ( ), instead, square brackets ( []) are used in array style :

var is  invalidPropertyNamesSample = {  
    "" :  "An empty string" ,  
    "!" :  "Fire!"  
  }  
  console.log (invalidPropertyNamesExample. "" );   // SyntaxError: Unexpected string  
  console.log (invalidPropertyNamesExample [ "" ]);  // an empty string  
  console.log (invalidPropertyNamesExample!);    // SyntaxError: Unexpected token!  
  console.log (invalidPropertyNamesExample [ "!" ]); // Fire!
var  object = {a:  "pear" , 2:  "two" };  
  console.log (obj.a);    // pear  
  console.log (object [2]);   // two  
  //console.log(object.2); // Error: missing) after argument list  
  //console.log(exb=a]); // Error: a is not defined  
  console.log (object [ "a" ]); // pear  
  console.log (object [ "2" ]); // two

The operators

Arithmetic operators are used for calculations with JavaScript.

<script>
    var x;
    x = (5 + 1) * 10;
    alert(x);
</script>

Regular Expression (RegExp) values

A regular expression value /is expressed between the slash ( ) characters. Below is a sample regular expression:

var  re = / ab + c /;

Comments

Not all JavaScript code is processed by the browser.

The double slash // after, / * between * / is handled as comment code by the browser.

<script>
    var x;
    x = 5;
   // alert(x * 10);
</script>

Comments are not processed by the browser.

Identifiers

JavaScript variables, functions, and tags are called descriptors.

Descriptor rules are the same as for most programming languages.

The first character of the JavaScript identifier can be a letter, underscore (_), or dollar sign ($).

Subsequent characters can be characters, numbers, underscores, or dollar signs.

Letter sensitivity

JavaScript identifiers are case sensitive.

Example world and Earth are two different variables.

<script>
    var earth, Earth;
    earth = "Hello Sir Node";
    Earth = "Hello Sir Node";
    alert(Earth);
</script>

Descriptive Nomenclature

Using the commonly used descriptor when defining variables, functions or tags that contain more than one word with JavaScript will increase the readability of the code.

Using underscores between each word;

hello_world, hello_javascript, birth_date

Using capital letters at the beginning of each word;

Hello World, HelloJavascript, Date of Birth

Using capital letters except the first letter of the variable;

hello world, hello javascript, birthdate

Working Way

When developing with JavaScript, more than one code is often written.

JavaScript codes are interpreted from top to bottom.

In the example below, variables a, b, c are defined, respectively, and values ​​are assigned to variables.

In the last line, the variable c is written on the screen.

<script>
    var a, b, c;
    a = 5;
    b = 6;
    c = a + b;
    alert(c);
</script>

Separating JavaScript Codes

When writing code with JavaScript, we can separate the codes with semicolons.

<script>
    var a, b, c;
    a = 5;
    b = 6;
    c = a + b;
</script>

JavaScript codes can be written on a single line when separated by semicolons.

<script>
    var a, b, c; a = 5; b = 6; c = a + b;
</script>

Separating JavaScript codes with semicolons is not mandatory.

Using it minimizes mistakes.

Leave Blank

When writing code with JavaScript, gaps between codes are ignored.

To make the codes more readable, you can leave a space between each code.

name = "Sir Node";
name="Sir Node";

You can leave spaces between operators.

You can leave spaces between operators.
result = 5 + 6;

Code Length

Long JavaScript codes reduce readability.

Splitting long JavaScript codes into multiple lines will improve readability.

Code Blocks

JavaScript codes with curly brackets {. It can be grouped between code blocks using}.

function printName() {
    var name = "Sir Node"
    alert(name);
}

Splitting codes into blocks improves code readability.

You can find other JavaScript Tutorials here…

If you want to learn more examples about javascript variables, click here.