JavaScript beginner


Antonio Pierro @antonio_pierro_


Per consigli, suggerimenti, eventuali errori o altro potete scrivere una email a antonio.pierro[at]gmail.com

What is JavaScript?

  • JavaScript (often shortened to JS) is a cross-platform programming language for Web pages.
  • The JavaScript standard is ECMAScript.
  • All modern browsers fully support ECMAScript 5.1

JavaScript values

  • JavaScript recognizes the five following types of primitive values:
Type Examples of typed values / Notes
Numbers 42, 3.14159
Logical true / false
Strings "I am a string"
null a special keyword denoting a null value; null is also a primitive value. Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant
undefined a top-level property whose value is undefined; undefined is also a primitive value.

JavaScript Variables

  • You use variables as symbolic names for values in your application.
  • You can declare a variable in this way:
    • With the keyword "var". For example,
      var x = 42;
    • A variable declared using the var statement with no initial value specified has the value undefined.
      var x;
      console.log(x); // undefined

JavaScript Operators

  • JavaScript has both binary and unary operators, and one special ternary operator, the conditional operator.
  • A binary operator requires two operands, one before the operator and one after the operator:
    x * y;
  • A unary operator requires a single operand, either before or after the operator:
    x++;

Comparison operators

Operator Description Examples
Equal (==) Returns true if the operands are equal. 3 == 3
Not equal (!=) Returns true if the operands are not equal. 3 != 2
Greater than (>) Returns true if the left operand is greater than the right operand. 3 > 2
Less than (<) Returns true if the left operand is less than the right operand. 2 < 1

JavaScript functions

  • A function definition consists of the "function" keyword, followed by
    • the name of the function
    • a list of arguments to the function, enclosed in parentheses and separated by commas
    • the JavaScript statements that define the function, enclosed in curly braces, "{ }".

JavaScript function example

function square(number) {
  return number * number;
}

function sum(x, y) {
  return x + y;
}

Calling functions

  • Defining a function does not execute it.
  • Calling the function actually performs the specified actions with the indicated parameters.
  • For example, if you define the function square, you could call it as follows:
    square(5);

What is jQuery?

  • jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML
  • jQuery is the most popular JavaScript library in use today.

Including the library

  • The jQuery library is a single JavaScript file
  • It is possible to include jQuery directly from content delivery networks:
    <script src="http://code.jquery.com/jquery-1.11.min.js">

Usage style

  • You can use jQuery via the "$"" function.
  • Examples:
    $('body').append('ciao2');
    
    $('body').html('ciao');
    
    $('img').attr('src', 'image.jpg')
    

jQuery Ajax

  • The jQuery library has a full suite of Ajax capabilities.
  • The functions and methods allow us to load data from the server without a browser page refresh.

jQuery Ajax example

$.ajax({
  url: "test.html"
}).done(function(content) {
  $('body').html(content);
});

Acronymous

  • URL: uniform resource locator. It is also known as web address.

References

  1. JavaScript | MDN