Monday, August 24, 2020

JavaScript Syntax

Whitespace and new lines

JavaScript ignores spaces, tabs, and newlines that appear in JavaScript code. You are free to format and indent your programs in a neat and consistent way that makes the code easy to read and understand.

Semicolons

S tatements in JavaScript generally end with a semicolon character, similar to C, C++, and Java. JavaScript, however, allows you to omit this semicolon if each of your statements are placed on a separate line.

  • Example 1

  •       varA  = "Learn"
  •       varB = "JAScript"

  • Example 2

  •       varA  = "Learn" varB = "JAScript"

  • Example 3

  •       varA  = "Learn"; varB = "JAScript";

In the above examples Example 1 and 3 will run while example 2 will generate error.

Case Sensitivity

JavaScript is a case-sensitive language. This means that the language keywords, variables, function names, and any other identifiers must always be typed in consistency.

  • Example 4

  •     var myVar = 5;
  •     var myvar = 10;
  •     print(myVar);
  •     print(myvar);

        Example 5

       function myFunction(){
         return 5;
  • };

  •  function myfunction(){
        return 10;
  • };

In the above example myVar and myvar and two different variables and they will print 5 and 10 respectively. In example 5, the two functions are totally different


Comments in JavaScript

JavaScript supports both C-style and C++-style comments.

    1. Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.
    2. Any text between the characters /* and */ is treated as a comment. This may span multiple lines.

  • Example 6

  • //This is a single line comment
  • var val = 5;
  • var name = "Tree";
  • var combined = val+name;
  • print(combined);
  • /*
  • This
  • is
  • a
  • multiline
  • comment
  • */

Friday, August 21, 2020

Outputting strings in JAScript

For android projects
There are many ways to output a string in JAScript. The following are some of the ways you can display a string:-

    1. print();
    2. System.out.println()
    3. alert();
    4. popup()


For HTML Projects

      1 document.write()
       2 console.log()
       3 element.innerText


Examples

Example 1

  • var a = "JAScript";
  • print(a);


To see the output of the example above open menu, then select console.

Example 2

  • var System = Packages.java.lang.System;
  • var a = "JAScript";
  • System.out.println(a);

Example 2 will also output to console.

Example 3

  • var b = "JavaScript";
  • alert(b);

Example 3 will show an alert dialog containing the word JavaScript

Example 4

  • var c = "Popup";
  • popup(c);

Example 4 will show a toast with the content of variable c i.e Popup

JavaScript Syntax

Whitespace and new lines JavaScript ignores spaces, tabs, and newlines that appear in JavaScript code. You are free to fo...