Javascript (CodeAcademy) - Предавања v.2
Javascript (CodeAcademy) - Предавања v.2
JAVASCRIPT
What is Javascript
JavaScript is a loosely-typed client side scripting language that executes in the user's web browser. A web page
without JavaScript is unimaginable today. There are many open source application development frameworks based on
JavaScript.
JavaScript is a loosely-typed client side scripting language that executes in the user's browser. JavaScript
interact with html elements (DOM elements) in order to make interactive web user interface.
JavaScript implements ECMAScript standards, which includes core features based on ECMA-262
specification as well as other features which are not based on ECMAScript standards.
JavaScript can be used in various activities like data validation, display popup messages, handling different events of
DOM elements, modifying style of DOM elements etc. The following sample form uses JavaScript to validate data and
change color of form.
Javascript History
In early 1995, Brendan Eich from Netscape, took charge of design and implementation of a new language for
non-java programmers to give access of newly added Java support in Netscape navigator.
Eich eventually decided that a loosely-typed scripting language suited the environment and audience, web
designers and developers who needed to be able to tie into page elements (such as forms, or frames, or
images) without a bytecode compiler or knowledge of object-oriented software design. The dynamic nature
of the language led to it being named "LiveScript" but was quickly renamed to "JavaScript"
Javascript Engine
JavaScript engine in the browser interprets, compiles and executes JavaScript code which is in a web page. It
does memory management, JIT compilation, type system etc. Each browser includes different JavaScript
engines.
1
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
C# Java JavaScript
Strongly-Typed Strongly-Typed Loosely-Typed
Static Static Dynamic
Classical Inheritance Classical Inheritance Prototypal
Classes Classes Functions
Constructors Constructors Functions
Methods Methods Functions
Advantages of Javascript
1. JavaScript is easy to learn.
2. It executes on client's browser, so eliminates server side processing.
3. It executes on any OS.
4. JavaScript can be used with any type of web page e.g. PHP, ASP.NET, Perl etc.
5. Performance of web page increases due to client side execution.
6. JavaScript code can be minified to decrease loading time from server.
7. Many JavaScript based application frameworks are available in the market to create Single page web
applications e.g. ExtJS, AngularJS, KnockoutJS etc.
1. Browser
2. Editor
Browser
You can install any browser as per your preference e.g. Internet Explorer, Chrome, FireFox, Safari, Opera etc.
JavaScript works on any web browser on any OS.
Editor
You can write JavaScript code using a simple editor like Notepad. However, you can also install any open source
or licensed IDE in order to get IntelliSense support for JavaScript and syntax error/warning highlighter e.g.
2
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Visual Studio, Aptana, Eclipse etc. Prefer an editor which has built-in features of IntelliSense support and
syntax error highlighter for speedy development.
Online Editor
You can also use online editor to learn JavaScript e.g. plnkr.co, jsfiddle.net or jsbin.com.
Script Tag
Any type of client-side script can be written inside <script> tag in html. The script tag identifies a block of script
code in the html page. It also loads a script file with src attribute.
The JavaScript code can also be embedded in <script> tag as shown below.
<script>
</script>
Html 4.x requires type attribute in script tag. The type attribute is used to identify the language of script code
embedded within script tag. This is specified as MIME type e.g. text/javascript, text/ecmascript, text/vbscript
etc. So, for the JavaScript code, specify type="text/javascript" in the script tag in html 4.x page.
<script type="text/javascript">
</script>
Html 5 page does not require type attribute in the <script> tag, because in HTML 5 the default script language
is JavaScript
Script File
If you don't want to write JavaScript between script tag in a web page then you can also write JavaScript code
in a separate file with .js extension and include it in a web page using <script> tag and reference the file via
src attribute.
<script src="/PathToScriptFile.js"></script>
3
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
The script tag can appear any number of times in the <head> or <body> tag.
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>JavaScript Demo</title>
</head>
<body>
<h1> JavaScript Tutorials</h1>
</body>
// 1ST Approach
<script>
//write JavaScript code here.
</script>
// 2nd Approach
<script src="/PathToScriptFile.js"></<script> @* External JavaScript file *@
</html>
The browser loads all the scripts in head tag before loading and rendering body html. It is recommended to include scripts before
ending body tag if scripts are not required while window is loading.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>JavaScript Demo</title>
</head>
<body>
<h1> JavaScript Tutorials</h1>
<p>This is JavaScript sample.</p>
4
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
<script>
//write JavaScript code here..
</script>
<script src="/PathToScriptFile.js"></<script> @* External JavaScript file *@
</body>
</html>
Character Set
JavaScript uses the Unicode character set and so allows almost all characters, punctuations, and symbols.
Case Sensitive
JavaScript is a case sensitive scripting language. It means functions, variables and keywords are case sensitive.
For example, VAR is different from var, John is not equal to john.
String
String is a text in JavaScript. A text content must be enclosed in double or single quotation marks.
Example: string
<script>
</script>
Number
JavaScript allows you to work with any kind of numbers like integer, float, hexadecimal etc. Number
must NOT be wrapped in quotation marks.
Integer: 1000
Float: 10.2
Boolean
As in other languages, JavaScript also includes true or false as a boolean value.
5
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Semicolon
JavaScript statements are separated by a semicolon. However, it is not mandatory to end every statement
with a semicolon but it is recommended.
White space
JavaScript ignores multiple spaces and tabs.
Comments
A comment is a single or multiple lines, which give some information about the current program. Comments
are not for execution.
Write comment after double slashes // or write multiple lines of comments between/* and */
/* this
is multi line
comment*/
var two = 2;
var three = 3;
Keywords
Keywords are reserved words in JavaScript, which cannot be used as variable names or function names.
Keywords
var function if
else do while
6
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Keywords
in instanceOf typeOf
Alert Box
Use alert() function to display a popup message to the user. This popup will have OK button to close the popup.
The alert function can display message of any data type e.g. string, number, boolean etc. There is no need to
convert a message to string type.
Confirm Box
Sometimes you need to take the user's confirmation to proceed. For example, you want to take user's
confirmation before saving updated data or deleting existing data. In this scenario, use JavaScript built-in
function confirm(). The confirm() function displays a popup message to the user with two
buttons, OK and Cancel. You can check which button the user has clicked and proceed accordingly.
The following example demonstrates how to display a confirm box and then checks which button the user has
clicked.
7
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Prompt Box
Sometimes you may need to take the user's input to do further actions in a web page. For example, you want
to calculate EMI based on users' preferred tenure of loan. For this kind of scenario, use JavaScript built-in
function prompt().
Prompt function takes two string parameters. First parameter is the message to be displayed and second
parameter is the default value which will be in input text when the message is displayed.Syntax:
if (tenure != null) {
alert("You have entered " + tenure + " years" );
}
As you can see in the above example, we have specified a message as first parameter and default value "15"
as second parameter. The prompt function returns a user entered value. If user has not entered anything then
it returns null. So it is recommended to check null before proceeding.
Note: The alert, confirm and prompt functions are global functions. So it can be called using window object
like window.alert(), window.confirm() and window.prompt().
Javascript | Variable
Variable means anything that can vary. JavaScript includes variables which hold the data value and it can be
changed anytime.
JavaScript uses reserved keyword var to declare a variable. A variable must have a unique name. You can
assign a value to a variable using equal to (=) operator when you declare it or before using it.
Syntax:
var <variable-name>;
8
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
In the above example, we have declared three variables using var keyword: one, two and three. We have
assigned values to variables one and two at the same time when we declared it, whereas variable three is
declared but does not hold any value yet, so it's value will be 'undefined'.
two = 'two';
Note: It is Not Recommended to declare a variable without var keyword. It can accidently overwrite an existing global
variable.
Scope of the variables declared without var keyword become global irrespective of where it is declared. Global
variables can be accessed from anywhere in the web page.
var
one
=
1,
two
=
"two"
Loosely-typed Variables
C# or Java has strongly typed variables. It means variable must be declared with a particular data type, which
tells what type of data the variable will hold.
JavaScript variables are loosely-typed which means it does not require a data type to be declared. You can
assign any type of literal values to a variable e.g. string, integer, float, boolean etc..
9
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Javascript | Operators
JavaScript includes operators as in other languages. An operator performs some operation on single or
multiple operands (data value) and produces a result. For example 1 + 2, where + sign is an operator and 1 is
left operand and 2 is right operand. + operator adds two numeric values and produces a result which is 3 in
this case.yntax:
1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Assignment Operators
5. Conditional Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations between numeric operands.
Operator Description
10
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Operator Description
The following example demonstrates how arithmetic operators perform different tasks on operands.
x + y; //returns 15
y - x; //returns 5
x * y; //returns 50
y / x; //returns 2
x % 2; //returns 1
x++; //returns 6
x--; //returns 4
+ operator performs concatenation operation when one of the operands is of string type.
The following example shows how + operator performs operation on operands of different data types.
Example: + operator
var a = 5, b = "Hello ", c = "World!", d = 10;
a + b; // "5Hello "
b + c; // "Hello World!"
a + d; // 15
11
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Comparison Operators
JavaScript language includes operators that compare two operands and return Boolean value true or false.
Operators Description
> Checks whether left side value is greater than right side value. If yes then returns true otherwise false.
< Checks whether left operand is less than right operand. If yes then returns true otherwise false.
>= Checks whether left operand is greater than or equal to right operand. If yes then returns true otherwise false.
<= Checks whether left operand is less than or equal to right operand. If yes then returns true otherwise false.
The following example demonstrates how comparison operators perform different tasks.
a == c; // returns true
a == x; // returns true
a != b; // returns true
12
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Logical Operators
Logical operators are used to combine two or more conditions. JavaScript includes following logical operators.
Operator Description
&& && is known as AND operator. It checks whether two operands are non-zero (0, false, undefined, null or "" are
considered as zero), if yes then returns 1 otherwise 0.
|| || is known as OR operator. It checks whether any one of the two operands is non-zero (0, false, undefined, null
or "" is considered as zero).
! ! is known as NOT operator. It reverses the boolean result of the operand (or condition)
Assignment Operators
JavaScript includes assignment operators to assign values to variables with less key strokes.
Assignment
operators Description
+= Sums up left and right operand values and assign the result to the left operand.
-= Subtract right operand value from left operand value and assign the result to the
left operand.
*= Multiply left and right operand values and assign the result to the left operand.
/= Divide left operand value by right operand value and assign the result to the left
operand.
%= Get the modulus of left operand divide by right operand and assign resulted
modulus to the left operand.
13
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
x = y; //x would be 10
x += 1; //x would be 6
x -= 1; //x would be 4
x *= 5; //x would be 25
x /= 5; //x would be 1
x %= 2; //x would be 1
Ternary Operator
JavaScript includes special operator called ternary operator :? that assigns a value to a variable based on some
condition. This is like short form of if-else condition.Syntax:
Ternary operator starts with conditional expression followed by ? operator. Second part ( after ? and before :
operator) will be executed if condition turns out to be true. If condition becomes false then third part (after :)
will be executed.
Points to Remember:
1. JavaScript includes operators that perform some operation on single or multiple operands (data value) and
produce a result.
2. JavaScript includes various categories of operators: Arithmetic operators, Comparison operators, Logical
operators, Assignment operators, Conditional operators.
3. Ternary operator ?: is a conditional operator.
14
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
JavaScript includes primitive and non-primitive data types as per latest ECMAScript 5.1.
1. String
2. Number
3. Boolean
4. Null
5. Undefined
1. Object
2. Date
3. Array
JavaScript is a dynamic or loosely-typed language because a variable can hold value of any data type at any
point of time.
alert(myVar); // Steve
In the above example, myVar will hold last assigned value to it that is string "Steve".
15
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Javascript - String
String is a primitive data type in JavaScript. A string is textual content. It must be enclosed in single or double
quotation marks.
'Hello World'
A string can also be treated like zero index based character array.
str[0] // H
str[1] // e
str[2] // l
str[3] // l
str[4] // o
str.length // 11
Since, string is character index, it can be accessed using for loop and for-of loop.
for(var ch of str)
console.log(ch);
16
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Concatenation
A string is immutable in JavaScript, it can be concatenated using plus (+) operator in JavaScript.
If you want to include same quotes in a string value as surrounding quotes then use backward slash (\) before
quotation mark inside string value.
String object
Above, we assigned a string literal to a variable. JavaScript allows you to create a String object using
the new keyword, as shown below.
// or
In the above example, JavaScript returns String object instead of primitive string type. It is recommended to
use primitive string instead of String object.
17
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Caution:
Be careful while working with String object because comparison of string objects using == operator compares
String objects and not the values. Consider the following example.
str1 == str2; // false - because str1 and str2 are two different objects
str1 == str3; // true
str1 === str4; // true
typeof(str1); // object
typeof(str3); //string
String Properties
Property Description
String Methods
Method Description
charCodeAt(position) Returns a number indicating the Unicode value of the character at the given position (in
Number).
concat([string,,]) Joins specified string literal values (specify multiple strings separated by comma) and
returns a new string.
indexOf(SearchString, Returns the index of first occurrence of specified String starting from specified number
Position) index. Returns -1 if not found.
lastIndexOf(SearchString, Returns the last occurrence index of specified SearchString, starting from specified
Position) position. Returns -1 if not found.
match(RegExp) Search a string for a match using specified regular expression. Returns a matching array.
18
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Method Description
replace(searchValue, Search specified string value and replace with specified replace Value string and return
replaceValue) new string. Regular expression can also be used as searchValue.
slice(startNumber, Extracts a section of a string based on specified starting and ending index and returns a
endNumber) new string.
split(separatorString, Splits a String into an array of strings by separating the string into substrings based on
limitNumber) specified separator. Regular expression can also be used as separator.
substr(start, length) Returns the characters in a string from specified starting position through the specified
number of characters (length).
substring(start, end) Returns the characters in a string between start and end indexes.
Method Description
link() Wraps a string in <a>tag where href attribute value is set to specified string.
19
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Method Description
Javascript - Number
The Number is a primitive data type in JavaScript. Number type represents integer, float, hexadecimal, octal
or exponential value. First character in a Number type must be an integer value and it must not be enclosed
in quotation marks.
Number object
JavaScript also provides Number object which can be used with new keyword.
Caution: Be careful while working with the Number object because comparison of Number objects using ==
operator compares Number objects and not the values. Consider the following example.
num1 == num2; // false - because num1 and num2 are two different objects
num1 == num3; // true
num1 === num3;//false
typeof(num1); // object
typeof(num3); //number
20
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Number Properties
The Number type includes some default properties. JavaScript treats primitive values as object, so all the
properties and methods are applicable to both primitive number values and number objects.
Property Description
Number Methods
The following table lists all the methods of Number type
Method Description
Example:
var num = 100; Num.toExponential(2); // returns '1.00e+2'
Example:
var num = 100; Num.toFixed(2); // returns '100.00'
Example:
var num = 100; Num.toLocaleString(); // returns '100'
21
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Method Description
Example:
var num = 100; Num.toPrecision(4); // returns '100.0'
Example:
var num = 100; Num.toString(); // returns '100'
Javascript Boolean
Boolean is a primitive data type in JavaScript. Boolean can have only two values, true or false. It is useful in
controlling program flow using conditional statements like if..else, switch, while, do..while.
Example: Boolean
var YES = true;
var NO = false;
The following example demonstrates how a Boolean value controls the program flow using if condition.
Example: Boolean
var YES = true;
var NO = false;
if(YES)
{
alert("This code block will be executed");
}
if(NO)
{
alert("This code block will not be executed");
}
Example: Boolean
Boolean object
JavaScript includes Boolean object to represent true or false. It can be initialized using new keyword.
alert(bool); // true
JavaScript treats empty string (""), 0, undefined and null as false. Everything else is true.
Example: Boolean
var bool1 = new Boolean(""); // false
Boolean Methods
Primitive or Boolean object includes following methods.
Method Description
23
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Javascript | Object
Object is a non-primitive data type in JavaScript. It is like any other variable, the only difference is that an
object holds multiple values in terms of properties and methods. Properties can hold values of primitive data
types and methods are functions.
1. Object literal
2. Object constructor
Object Literal
The object literal is a simple way of creating an object using { } brackets. You can include key-value pair in { },
where key would be property or method name and value will be value of property of any data type or a
function. Use comma (,) to separate multiple key-value pairs.
24
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
You must specify key-value pair in object for properties or methods. Only property or method name without
value is not valid. The following syntax is invalid.
person.getFullName();
Note: An object's methods can be called using () operator e.g. person.getFullName(). Without (), it will return function
definition.
25
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Object Constructor
The second way to create an object is with Object Constructor using new keyword. You can attach properties
and methods using dot notation. Optionally, you can also create properties using [ ] brackets and specifying
property name as string.
Example
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
26
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
If you are not sure whether an object has a particular property or not, then use hasOwnProperty() method
before accessing properties.
Example: hasOwnProperty()
var person = new Object();
Example 2:
if(person.hasOwnProperty("firstName")){
person.firstName;
}
Example 3:
If (person.firstName != undefined) {
person.firstName = "James";
person["lastName"] = "Bond";
person.age = 25;
person.getFullName = function () {
return this.firstName + ' ' + this.lastName;
};
27
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Pass by Reference
Object in JavaScript passes by reference from one function to another.
changeFirstName(person)
person.firstName; // returns Steve
If, two objects point to the same object then the change made in one object will reflect in another object.
28
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Points to Remember :
1. JavaScript object is a standalone entity that holds multiple values in terms of properties and methods.
2. Object property stores a literal value and method represents function.
3. An object can be created using object literal or object constructor syntax.
4. Object literal:
var person = {
firstName: "James",
lastName: "Bond",
age: 25,
getFullName: function () {
return this.firstName + ' ' + this.lastName
}
};
5. Object constructor:
var person = new Object();
person.firstName = "James";
person["lastName"] = "Bond";
person.age = 25;
person.getFullName = function () {
return this.firstName + ' ' + this.lastName;
};
6. Object properties and methods can be accessed using dot notation or [ ] bracket.
7. An object is passed by reference from one function to another.
8. An object can include another object as a property.
29
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Javascript - Date
JavaScript provides Date object to work with date & time including days, months, years, hours, minutes,
seconds and milliseconds.
The following example shows how to display current date and time using Date object in JavaScript.
//or
As you can see in the above example, we can display current date and time either by calling Date as function
or creating an object with new keyword.
In order to work with date other than current date and time, we must create a date object by specifying
different parameters in the Date constructor.Syntax:
As per the above syntax, the following parameters can be specified in Date constructor.
• No Parameter: Date object will be set to current date & time if no parameter is specified in the constructor.
• Milliseconds: Milliseconds can be specified as numeric parameter. The date object will calculate date & time by
adding specified numeric milliseconds from mid night of 1/1/1970
• Date string: String parameter will be treated as a date and will be parsed using Date.parse method.
In the following example, date object is created by passing milliseconds in Date constructor. So date will be
calculated based on milliseconds elapsed from 1/1/1970.
30
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Specify any valid date as a string to create new date object for the specified date. The following example shows
various formats of date string which you can specify in a Date constructor.
You can use any valid separator in date string to differentiate date segments.
31
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Specify seven numeric values to create a date object with specified year, month and optionally date, hours,
minutes, seconds and milliseconds.
Example: Date
var dt = new Date(2014, 2, 3, 10, 30, 50, 800); // Mon Feb 03 2014 10:30:50
Date Methods
The JavaScript Date object includes various methods to operate on it. Use different methods to get different
segments of date like day, year, month, hour, seconds or milliseconds in either local time or UTC time.
Example 2:
date.getDay();// returns 3
date.getUTCDate();// returns 31
Date Formats
JavaScript supports ISO 8601 date format by default - YYYY-MM-DDTHH:mm:ss.sssZ
For example, use ToUTCString(), ToGMTString(), ToLocalDateString(), ToTimeString() methods to convert date
into respective formats.
date.toLocaleDateString();'2/10/2015'
32
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
date.toISOString(); '2015-02-10T10:12:50.500Z'
date.toTimeString(); '15:42:50'
To get date string in formats other than the ones listed above, you need to manually form the date string using
different Date methods. The following example converts date string to DD-MM-YYYY format.
var d = date.getDate();
var m = date.getMonth() + 1;
var y = date.getFullYear();
Note: Use third party JavaScript Date library like datejs.com or momentjs.com, if you want to work with Dates
extensively.
Parse Date
Use Date.parse() method to convert valid date string into milliseconds since midnight of 1/1/1970.
Example: Date.parse()
Date.parse("5/2/2015"); // 1430505000000
Compare Dates
Use comparison operators to compare two date objects.
33
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
Points to Remember :
Date object can be created using new keyword. e.g. var date = new Date();
Date can be created by specifying milliseconds, date string or year and month in Date constructor.
Date can be created by specifying date string in different formats using different separators.
Method Description
getDay() Returns the day of the week (0 - 6) for the specified date.
getTime() Returns the milliseconds as number since January 1, 1970, 00:00:00 UTC.
getTimezoneOffset() Returns the time zone offset in minutes for the current locale.
getUTCDate() Returns the day (1 - 31) of the month of the specified date as per UTC time zone.
getUTCDay() Returns the day (0 - 6) of the week of the specified date as per UTC timezone.
getUTCFullYear() Returns the four digits year of the specified date as per UTC time zone.
getUTCHours() Returns the hours (0 - 23) of the specified date as per UTC time zone.
getUTCMilliseconds() Returns the milliseconds (0 - 999) of the specified date as per UTC time zone.
getUTCMinutes() Returns the minutes (0 - 59) of the specified date as per UTC time zone.
getUTCMonth() Returns the month (0 - 11) of the specified date as per UTC time zone.
getUTCSeconds() Returns the seconds (0 - 59) of the specified date as per UTC time zone.
getYear() Returns the no of years of the specified date since 1990. This method is Deprecated
34
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
The following table lists all the set methods of Date object.
Method Description
setFullYear() Sets the four digit full year as number in the date object. Optionally set month and date.
setHours() Sets the hours as number in the date object. Optionally set minutes, seconds and milliseconds.
setMinutes() Sets the minutes as number in the date object. Optionally set seconds & milliseconds.
setMonth() Sets the month as number in the date object. Optionally set date.
setSeconds() Sets the seconds as number in the date object. Optionally set milliseconds.
setTime() Sets the time as number in the Date object since January 1, 1970, 00:00:00 UTC.
setUTCDate() Sets the day in the date object as per UTC time zone.
setUTCFullYear() Sets the full year in the date object as per UTC time zone
setUTCHours() Sets the hour in the date object as per UTC time zone
setUTCMilliseconds() Sets the milliseconds in the date object as per UTC time zone
setUTCMinutes() Sets the minutes in the date object as per UTC time zone
setUTCMonth() Sets the month in the date object as per UTC time zone
setUTCSeconds() Sets the seconds in the date object as per UTC time zone
setYear() Sets the year in the date object. This method is Deprecated
Returns the date segment from the specified date, excludes time.
toLocaleDateString() Returns the date segment of the specified date using the current locale.
toTimeString() Returns the time segment as a string from the specified date object.
35
JAVASCRIPT, CODE ACADEMY 2020 JORGOS LAMBRINIDIS
36