Lecture 1: Javascript, Data Types, Variables
- A scripting language interpreted in Browser (Client Side). Source code is delivered to client side - Also compiled in server and only result is delivered to client. Eg. Node.js - Also used in Game Engines (Unity), Mobile Applications, Query languages (Couch DB), Mongo DBExercises - Running JavaScript in Console - Running JavaScript in Browser Web Page
Data Types
- Stringvar greet = 'Hello, How are you?'; var review = "It's good"; var sentence = 'He said,"I\'m coming"';
-Number (64 bits)
var x = 50.00; // Written with decimals var y = 50; // Written without decimals var y = 123e4; // 1230000 var z = 123e-4; // 0.0123
Special Numbers Infinity, -Infinity, NaN (eg: Infinity-Infinity)
-Boolean
var x = true; var y = false; console.log(3>2); //true console.log("aa"<"bb"); //true isNaN("Hello"); //true
-undefined & null
var x; // x is undefined var a = null; // must be explicitly defined as null
Data Type Behaviour
-Automatic Data Conversionvar x = 1 + "Kathmandu"; var x = 1 + 2 + "Patan"; var x = "Kathmandu" + 2 + 1; console.log("10" - 2); console.log("ten"*10); console.log(false==0); null==undefined; //true null==0; //false
- type of Operator
var x; // x is undefined console.log(typeof(x));
var x = 5; // x is a Number var x = "John"; // x is a String
Variables
-Variables hold values.var num; num = 3; var name = "Modi", age = 60; var result = num *3; $y = "me";
-Variable Names -Begin with letters, $ or _ -Only contain letters, numbers, $ and _ -Case sensitive -Dont use Keywords
break case catch class const continue debugger default delete do else enum export extends false finally for function if implements import in instanceof interface let new null package private protected public return static super switch this throw true try typeof var void while with yield
Exercise
-Ex 1 Take first name, last name and birth year from a user and calculate his age. Show an output that says " Firstname LastName, you are NN years old". Use getFullYear() method of Date object to get current year. Do this task in the console and in the browser as well. For help - http://www.w3schools.com/js/js_dates.asp…
- Ex 2 Modify solution of Ex 1 to ask birthmonth and birthdate along with birth year of the user. Use other Methods of Date object to calculate exact age (years, month and days) of the user. Seek help from http://www.w3schools.com/js/js_date_meth…