JavaScript


Script languages


Script languages are programming languages that are mainly used to automate tasks in other programs or at the operating system level. Unlike compiler languages, which must be translated into machine code before they can be executed, scripting languages are executed directly by the operating system or by an interpreter. This means that scripting languages are usually faster to write and test than compiler languages, but also less powerful and usually slower to execute.


Some examples of scripting languages are:


  • Python
  • Ruby
  • Perl
  • Bash
  • PowerShell
  • JavaScript

  • Scripting languages are commonly used for automating system management tasks, building web applications or integrating applications. They are also popular for data processing and data science, as they are usually easy to read and write and provide a variety of libraries and frameworks for these purposes.

    JavaScript


    JavaScript is a programmable language that is mainly used to design web pages. It is supported by most modern web browsers and can be used to implement functions such as animations, games, forms and other interactive elements on web pages. JavaScript was originally developed by Netscape and is now one of the most widely used programming languages on the Internet. It is a client-side language, which means that it runs on the user's computer rather than on the server, which makes it very fast and allows the user to interact directly with the browser. In our case, we embed JavaScript in HTML.

    Implementation into HTML


    There are several ways to use JavaScript in an HTML page. One way is to insert the JavaScript code directly into the HTML code by writing it in a <script> tag. Here is an example:




    The script tag can also be used in the head of the web page. In this case, the JavaScript code is executed as soon as the page is loaded:




    An external JavaScript file can also be used. To do this, the src attribute of the <script> tag must be changed. An example is here


    Hello World! in JavaScript


    As with most websites, there is also a classic Hello World! programme. In this case, a web page is created and an alert is created by JavaScript in the body. This contains the words "Hello World! The browser creates a small alert window that appears automatically when the web page is opened.




    So we have also got to know our first method directly. Later, we will get to know other methods.

    Comments


    There are two types of comments: single-line comments and multi-line comments. Single-line comments start with // and continue until the end of the line. Multi-line comments start with /* and end with */. Everything between the start and end markers is considered to be a comment.


    Variables


    There are two types of comments: single-line comments and multi-line comments. Single-line comments start with // and continue until the end of the line. Multi-line comments start with /* and end with */. Everything between the start and end markers is considered to be a comment.


    1. To declare a variable in JavaScript, you use the “var” keyword followed by the name of the variable.
    2. You can also assign a value to the variable when you declare it.
    3. In addition to the “var” keyword, JavaScript also has the “let” and “const” keywords, which can be used to declare variables. The “let” keyword is used to declare a variable that can be reassigned, while the “const” keyword is used to declare a variable that cannot be reassigned. It's generally a good idea to use “const” for variables that don't need to be reassigned, and “let” for variables that do. This can help prevent accidental reassignments and make your code easier to understand.
    4. You can also declare multiple variables at once by separating their names with commas.



    Names of variables, functions, and other identifiers are case sensitive.


    In JavaScript, there are several different data types that are comparable to Java. Some of the most common data types are:


  • string: a sequence of characters, represented by enclosing the characters in single or double quotes. For example: "hello" or 'world'.
  • number: a numerical value. JavaScript has only one data type for numbers, which can represent both integers and floating-point values. For example: 10, 3.14, or -5.
  • boolean: a value that can be either true or false.
  • null: a special value that represents the absence of a value.
  • undefined: a special value that represents a variable that has not been assigned a value.
  • symbol: a unique, immutable data type that can be used to represent a unique identifier.
  • object: a collection of key-value pairs, representing a complex data structure.



  • After the declaration, the computer allocates memory space for the content of the variables. When the programme ends, the content of the variables can be deleted again. Computers often waste memory and the programmer must always know exactly what kind of data is stored in which variable to avoid errors. In addition, it is not always clear to the computer what purpose the combination of two data types has, so JavaScript treats all variables as strings when in doubt.

    Calculations


    The same basic principles as in Java are also used for calculations. The assignment is made via =.


    Various types of calculations are performed using arithmetic operators such as +, -, *, /, and %.




    You can also use the “Math” object to perform more advanced calculations:




    Note: JavaScript follows the standard order of operations (also known as the PEMDAS rule).


    The multiplication (b * c) will be performed before the addition (a +), because multiplication has a higher precedence than addition. You can use parentheses to override the default order of operations and specify the desired order of evaluation.




    Here is a list from arithmetic operators from our lectures:


    Increment/Decrement


    The increment operator (++) is used to increase the value of a variable by one. The operator can be placed before (prefix) or after (postfix) the variable. The decrement operator (--) can also be used to decrease the value of a variable by one. The operator can also be placed before (prefix) or after (postfix) the variable.


    The difference between prefix and postfix increment/decrement is the order in which the operation is performed. In the prefix form, the operation is performed before the value of the variable is returned. In the postfix form, the operation is performed after the value of the variable is returned. The difference between prefix and postfix increment/decrement is the order in which the operation is performed. In the prefix form, the operation is performed before the value of the variable is returned. In the postfix form, the operation is performed after the value of the variable is returned.


    It's important to note that the increment and decrement operators only work with variables, not with constants or expressions.


    Conditional Statements


    Conditional statements are used to perform different actions based on different conditions. It allows to execute a block of code only if a specific condition is met. It again reflects the basics of Java.


    Basically there are three types of conditional statements in JavaScript


    1. if statement: The ”if” statement is the most basic form of a conditional statement. It allows you to specify a condition, and if that condition is true, a block of code will be executed.
    2. if...else statement: The “if...else” statement allows you to specify an additional block of code to be executed if the condition is false.
    3. if...else if...else statement: The “if...else if...else” statement allows you to specify multiple conditions and specify a different block of code for each condition.



    In all of these examples, condition is a “Boolean” expression that evaluates to either true or false. The code inside the curly braces “{ }” will be executed only if the condition is true


    In order compare values we need to use comparison operators, which are listed in the next page.


    Comparison operations


    Comparison operators are used to compare two values and determine whether one is greater than, less than, equal to, or not equal to the other. They are used in conditional statements and loops to control the flow of a program


    Here is a list of the common comparison operators from our lecture:




    Examples:




    Note that the == operator performs type coercion, meaning that it converts the operands to the same type before making the comparison. For example, 2 == '2' returns true because the string '2' is coerced to the number 2 before the comparison. On the other hand, the === operator does not perform type coercion and only returns true if the operands are of the same type and have the same value. (source: https://itnext.io/javascript-type-coercion-implicit-conversion-and-why-to-use-and-avoid-ea9a38ecc777)

    Loops


    In JavaScript, a loop is a control flow statement that allows you to execute a block of code multiple times in a controlled and defined manner. There are several types of loops in JavaScript.


    1. for loops: These loops execute a block of code a specified number of times.
    2. while loops: The “if...else” statement allows you to specify an additional block of code to be executed if the condition is false.
    3. do...while loops: These loops are similar to while loops, but the code block is executed at least once, and then the condition is checked.
    4. for...in loops: : These loops are used to iterate over the properties of an object.
    5. for...of loops: : These loops are used to iterate over iterable objects, such as arrays and strings.

    Break Statement


    The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement. It includes an optional label that allows the program to break out of a labeled statement. The break statement needs to be nested within the referenced label. The labeled statement can be any block statement; it does not have to be preceded by a loop statement.


    A break statement, with or without a following label, cannot be used within the body of a function that is itself nested within the current loop, switch, or label statement that the break statement is intended to break out of. (source: https://developer.mozilla.org)