SlideShare a Scribd company logo
1 of 51
JavaScript Basics
Adam Crabtree
(Adapted from Joar Gullestad Pettersen’s
http://slideshare.net/Peanuts_Stavanger/javascript-basics-29353026)
JavaScript Data Types
String, Number, Boolean, Array, Object, Null, Undefined
Dynamic Types
• var x;
Dynamic Types
var x;

// x is: undefined
Dynamic Types
var x;

// x is: undefined

x = 5;

// x is set to a Number: 5
Dynamic Types
var x;

// x is: undefined

x = 5;

// x is set to a Number: 5

x = "Bond";

// x is changed to a String: "Bond"
Strings
var car = "Telsa Model S";
var car2 = 'Tesla Model S';
Numbers
• var x = 42;
• var y = 13.37;
• var z = 10e3;

// Written without decimals
// Written with decimals
// Exponential notation
Booleans
• var x = true;
• var y = false;
Array
var frukt = new Array();
frukt[0] = "eple";
frukt[1] = "banan";
frukt[2] = "pære";
["eple", "banan", "pære"]
Array 2
var frukt = new Array("eple", "banan", "pære");
["eple", "banan", "pære"]
Array 3
var frukt = ["eple", "banan", "pære"];
["eple", "banan", "pære"]
Array 4
• var frukt = ["eple", "banan", "pære"]
•
•
•
•

frukt.pop();

// ["eple", "banan"]

frukt.push("tomat");

// ["eple", "banan", "tomat"]

frukt.shift();

// ["banan", "tomat"]

frukt.unshift("tomat"); // ["tomat", "banan", "tomat"]
Objects
• Everything is an Object
Objects
•
•
•
•
•

Everything is an Object
Booleans can be objects or primitive data treated as objects
Numbers can be objects or primitive data treated as objects
Strings are also objects or primitive data treated as objects
Dates, Maths, Regular expressions, Arrays and functions are always objects
Object literal
An object is just a special kind of
data, with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
id: 5
};
Object literal
An object is just a special kind of data,
with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
id: 5
};
person.id; // 5
Object literal
An object is just a special kind of data,
with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
address: {

street: "Strandsvingen",
number: "14B"
}
};

person.address.street; // "Strandsvingen"
Functions
a block of code that will be executed when "someone" calls it:
function add(a, b) {
return a + b;
}
var add = function(a, b) {
return a + b;
}
Object Constructor
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
var randomPerson = new Person("John", "Doe");
Object
var randomPerson = new Object();
randomPerson.firstName = "John";
randomPerson.lastName = "Doe";
Boolean expressions
if (a == 2) {//if this is true
//do this...
}
Type coercion
• When JavaScript are unsure what you mean, It makes a guess
• Example:
if ('false') { console.log("true"); }

• «A String is obviously not true or false, you probably
mean true!»
True! 
• if (true) { alert("true"); } // alerts "true"
• if ('false') { alert("true"); } // alerts "true"

• if ([]) { alert("true"); } // alerts "true"
• if (5) { alert("true"); } // alerts "true"

• if ({}) { alert("true"); } // alerts "true"
False 
• if (false) { alert("false"); } // does nothing
• if (0) { alert("false"); } // does nothing

• if (null) { alert("false"); } // does nothing
• if (undefined) { alert("false"); } // does nothing

• if ("") { alert("false"); } // does nothing
More type coercion 
1 == "1"
true == "1"
false == "0"
More type coercion 
1 == "1"
true
true == "1"
false == "0"
More type coercion 
1 == "1"
true
true == "1"

true
false == "0"
More type coercion 
1 == "1"
true
true == "1"

true
false == "0"
true
More type coercion
1 == "1"
true
true == "1"

true
false == "0"
true
false == "0" == 1 == true == [] == ""
More type coercion 
1 == "1"
true
true == "1"

true
false == "0"
true
false == "0" == 1 == true == [] == ""
true
Confusing?
• Solution?
Confusing?
• Solution?

• Avoid bugs| and use: ===
===
1 == true
true
1 === true
false
1 == "1"
true
1 === "1"
false
Scope & global variables
• C#/Java: anything inside curly brackets, {} , defines a scope
• Example:
if (true)

{

var scopeVariable = "Test";
}
scopeVariable = "Test2"; // variable not defined
Scope & global variables
• Javascript: only functions define a new scope
• Example:
if (true) {
var scopeVariable = "Test";
}
scopeVariable; // value: "Test";
Scope & global variables
function scope1() {
var member; //is a member of the scope defined by the function example
//this function is also part of the scope of the function example

var innerScope = function() {
member= 12; // traverses scope and assigns member in scope1 to 12
};
};
Scope & global variables
function scope1() {
var member; //is a member of the scope defined by the function example
//this function is also part of the scope of the function example

var innerScope = function() {
var member= 12; // defines member in this scope and do not traverse
};
};
Scope & global variables
function scope1() {
var member;

//is a member of the scope defined by the function example

//this function is also part of the scope of the function example
var bar = function() {
member= 12; // traverses scope and assigns scope1member.foo to 12
};
};
function scope2() {
member = 15; // traverses scope and assigns global.member to 15
}
Namespaces
• Not built into JavaScript
• Problem?
JavaScript (Ad-hoc) namespace
var Peanuts = {};

// Object used as namespace
JavaScript (Ad-hoc) namespace
var Peanuts = Peanuts || {}; // in case it already
exists
«Classes» and «methods» ?
var Peanuts = Peanuts || {};
Peanuts.Calculator = {
add: function (a,b) {
return a + b;

},
subtract: function () {
return a - b;
}
};

Peanuts.Calculator.add(1, 2); // 3
Immediately invoked function expressions
• (function () {
•
// logic/code here
• })();
Why?
• Executes an expression and therefore code immediately
• Creates «privacy» for your code (function scope ftw!)
How?
• JavaScript, parenthesis can’t contain statements.
• When the parser encounters the function keyword, it knows to
parse it as a function expression and not a function declaration.
Immediately invoked function expressions
•
•
•
•
•

(function () {
// logic/code here
})();
The key thing about JavaScript expressions is that they return values.
To invoke the function expression right away we just need to tackle a couple
of parentheses on the end(like above).
Immediately invoked function expressions
• (function (innerValue) {
•
// logic/code here
• })(outerValue);
Revealing module pattern
var Peanuts = Peanuts || {};
Peanuts.Calculator = function () {
var add = function(a, b) {
return a + b;
};
var subtract = function(a, b) {

return a - b;
};
return {
add: add,
subtract: subtract
};
};

Peanuts.Calculator().add(1, 2); // 3
Revealing module pattern 2
var Peanuts = Peanuts || {};
Peanuts.Calculator = (function () {
var add = function(a, b) {
return a + b;

};
return {
add: add,
};
})();

Peanuts.Calculator.add(1, 2); // 3
Revealing module pattern 3
var Peanuts = Peanuts || {};
Peanuts.Calculator = (function () {
var PI = 3.14; // private variable!
var getCircleArea = function(r) {
return PI * r * r;

};
return {
getCircleArea: getCircleArea // public method
};
})();
Peanuts.Calculator.getCircleArea(2); // 12.56

More Related Content

What's hot

Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript BootcampAndreCharland
 
javascript objects
javascript objectsjavascript objects
javascript objectsVijay Kalyan
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Aaron Gustafson
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesDoris Chen
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Librariesjeresig
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript BasicsMindfire Solutions
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Jaroslaw Palka
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScriptSimon Willison
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - ObjectsWebStackAcademy
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic FunctionsWebStackAcademy
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptzand3rs
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and ArraysWebStackAcademy
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of JavascriptTarek Yehia
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptStoyan Stefanov
 

What's hot (20)

Ajax and JavaScript Bootcamp
Ajax and JavaScript BootcampAjax and JavaScript Bootcamp
Ajax and JavaScript Bootcamp
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
javascript objects
javascript objectsjavascript objects
javascript objects
 
Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]Fundamental JavaScript [UTC, March 2014]
Fundamental JavaScript [UTC, March 2014]
 
Performance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best PracticesPerformance Optimization and JavaScript Best Practices
Performance Optimization and JavaScript Best Practices
 
Secrets of JavaScript Libraries
Secrets of JavaScript LibrariesSecrets of JavaScript Libraries
Secrets of JavaScript Libraries
 
A Deeper look into Javascript Basics
A Deeper look into Javascript BasicsA Deeper look into Javascript Basics
A Deeper look into Javascript Basics
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014Patterns for JVM languages - Geecon 2014
Patterns for JVM languages - Geecon 2014
 
A Re-Introduction to JavaScript
A Re-Introduction to JavaScriptA Re-Introduction to JavaScript
A Re-Introduction to JavaScript
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
JavaScript - Chapter 8 - Objects
 JavaScript - Chapter 8 - Objects JavaScript - Chapter 8 - Objects
JavaScript - Chapter 8 - Objects
 
JavaScript Basics
JavaScript BasicsJavaScript Basics
JavaScript Basics
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Object Oriented Programming in JavaScript
Object Oriented Programming in JavaScriptObject Oriented Programming in JavaScript
Object Oriented Programming in JavaScript
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Beginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScriptBeginning Object-Oriented JavaScript
Beginning Object-Oriented JavaScript
 

Viewers also liked

14526610 english-banana-the-second-book-
14526610 english-banana-the-second-book-14526610 english-banana-the-second-book-
14526610 english-banana-the-second-book-incasong
 
Participants List For Jsm
Participants List For JsmParticipants List For Jsm
Participants List For JsmJADE aisbl
 
Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)Goodrich Global
 
Tulospohjainen Markkinointi Netissä
Tulospohjainen Markkinointi NetissäTulospohjainen Markkinointi Netissä
Tulospohjainen Markkinointi NetissäDarwin Oy
 
Asiakkaan Kohtaaminen
Asiakkaan KohtaaminenAsiakkaan Kohtaaminen
Asiakkaan KohtaaminenDarwin Oy
 
Hyvinvointituotteen markkinointi - miten onnistut aina!
Hyvinvointituotteen markkinointi - miten onnistut aina!Hyvinvointituotteen markkinointi - miten onnistut aina!
Hyvinvointituotteen markkinointi - miten onnistut aina!Darwin Oy
 
Burns night (3ºc 2012 2013
Burns night (3ºc  2012 2013Burns night (3ºc  2012 2013
Burns night (3ºc 2012 2013Rocio Torres
 
как изменился уровень жизни россиян 2011
как изменился уровень жизни россиян 2011как изменился уровень жизни россиян 2011
как изменился уровень жизни россиян 2011SalesDog
 
Sosiaalisen median case-kimara
Sosiaalisen median case-kimaraSosiaalisen median case-kimara
Sosiaalisen median case-kimaraDarwin Oy
 
Green Energy Incentives Workbook
Green Energy Incentives WorkbookGreen Energy Incentives Workbook
Green Energy Incentives WorkbookDarren Welker, CPA
 
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassa
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassaOnko yrityksellä mitään järkeä olla sosiaalisessa mediassa
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassaDarwin Oy
 
Recognition of unistroke gesture sequences
Recognition of unistroke gesture sequencesRecognition of unistroke gesture sequences
Recognition of unistroke gesture sequencesArvind Krishnaa
 
Economics of Green Growth & National Innovation Strategies
Economics of Green Growth & National Innovation StrategiesEconomics of Green Growth & National Innovation Strategies
Economics of Green Growth & National Innovation StrategiesCambridgeIP Ltd
 

Viewers also liked (20)

14526610 english-banana-the-second-book-
14526610 english-banana-the-second-book-14526610 english-banana-the-second-book-
14526610 english-banana-the-second-book-
 
Vchitel_projekt
Vchitel_projektVchitel_projekt
Vchitel_projekt
 
Participants List For Jsm
Participants List For JsmParticipants List For Jsm
Participants List For Jsm
 
Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)
 
Tulospohjainen Markkinointi Netissä
Tulospohjainen Markkinointi NetissäTulospohjainen Markkinointi Netissä
Tulospohjainen Markkinointi Netissä
 
♥♥♥
♥♥♥♥♥♥
♥♥♥
 
Asiakkaan Kohtaaminen
Asiakkaan KohtaaminenAsiakkaan Kohtaaminen
Asiakkaan Kohtaaminen
 
Hyvinvointituotteen markkinointi - miten onnistut aina!
Hyvinvointituotteen markkinointi - miten onnistut aina!Hyvinvointituotteen markkinointi - miten onnistut aina!
Hyvinvointituotteen markkinointi - miten onnistut aina!
 
Chowka bhara
Chowka bharaChowka bhara
Chowka bhara
 
Unit 0
Unit 0Unit 0
Unit 0
 
Burns night (3ºc 2012 2013
Burns night (3ºc  2012 2013Burns night (3ºc  2012 2013
Burns night (3ºc 2012 2013
 
как изменился уровень жизни россиян 2011
как изменился уровень жизни россиян 2011как изменился уровень жизни россиян 2011
как изменился уровень жизни россиян 2011
 
Sosiaalisen median case-kimara
Sosiaalisen median case-kimaraSosiaalisen median case-kimara
Sosiaalisen median case-kimara
 
Modulo 2
Modulo 2Modulo 2
Modulo 2
 
Green Energy Incentives Workbook
Green Energy Incentives WorkbookGreen Energy Incentives Workbook
Green Energy Incentives Workbook
 
Analogical thinking
Analogical thinkingAnalogical thinking
Analogical thinking
 
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassa
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassaOnko yrityksellä mitään järkeä olla sosiaalisessa mediassa
Onko yrityksellä mitään järkeä olla sosiaalisessa mediassa
 
Recognition of unistroke gesture sequences
Recognition of unistroke gesture sequencesRecognition of unistroke gesture sequences
Recognition of unistroke gesture sequences
 
Peta persoalan
Peta persoalanPeta persoalan
Peta persoalan
 
Economics of Green Growth & National Innovation Strategies
Economics of Green Growth & National Innovation StrategiesEconomics of Green Growth & National Innovation Strategies
Economics of Green Growth & National Innovation Strategies
 

Similar to LinkedIn TBC JavaScript 100: Intro

Similar to LinkedIn TBC JavaScript 100: Intro (20)

JavaScript For CSharp Developer
JavaScript For CSharp DeveloperJavaScript For CSharp Developer
JavaScript For CSharp Developer
 
Stuff you didn't know about action script
Stuff you didn't know about action scriptStuff you didn't know about action script
Stuff you didn't know about action script
 
JavaScript Bootcamp
JavaScript BootcampJavaScript Bootcamp
JavaScript Bootcamp
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
DIWE - Programming with JavaScript
DIWE - Programming with JavaScriptDIWE - Programming with JavaScript
DIWE - Programming with JavaScript
 
Javascript status 2016
Javascript status 2016Javascript status 2016
Javascript status 2016
 
Java script object model
Java script object modelJava script object model
Java script object model
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
JavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuanJavaScript Beginner Tutorial | WeiYuan
JavaScript Beginner Tutorial | WeiYuan
 
Basics of Javascript
Basics of JavascriptBasics of Javascript
Basics of Javascript
 
Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3Fii Practic Frontend - BeeNear - laborator3
Fii Practic Frontend - BeeNear - laborator3
 
Wakanday JS201 Best Practices
Wakanday JS201 Best PracticesWakanday JS201 Best Practices
Wakanday JS201 Best Practices
 
JavaScript Literacy
JavaScript LiteracyJavaScript Literacy
JavaScript Literacy
 
Java script for web developer
Java script for web developerJava script for web developer
Java script for web developer
 
An introduction to javascript
An introduction to javascriptAn introduction to javascript
An introduction to javascript
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
Week3
Week3Week3
Week3
 
Surviving javascript.pptx
Surviving javascript.pptxSurviving javascript.pptx
Surviving javascript.pptx
 
Goodparts
GoodpartsGoodparts
Goodparts
 

Recently uploaded

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 

Recently uploaded (20)

Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 

LinkedIn TBC JavaScript 100: Intro

  • 1. JavaScript Basics Adam Crabtree (Adapted from Joar Gullestad Pettersen’s http://slideshare.net/Peanuts_Stavanger/javascript-basics-29353026)
  • 2. JavaScript Data Types String, Number, Boolean, Array, Object, Null, Undefined
  • 4. Dynamic Types var x; // x is: undefined
  • 5. Dynamic Types var x; // x is: undefined x = 5; // x is set to a Number: 5
  • 6. Dynamic Types var x; // x is: undefined x = 5; // x is set to a Number: 5 x = "Bond"; // x is changed to a String: "Bond"
  • 7. Strings var car = "Telsa Model S"; var car2 = 'Tesla Model S';
  • 8. Numbers • var x = 42; • var y = 13.37; • var z = 10e3; // Written without decimals // Written with decimals // Exponential notation
  • 9. Booleans • var x = true; • var y = false;
  • 10. Array var frukt = new Array(); frukt[0] = "eple"; frukt[1] = "banan"; frukt[2] = "pære"; ["eple", "banan", "pære"]
  • 11. Array 2 var frukt = new Array("eple", "banan", "pære"); ["eple", "banan", "pære"]
  • 12. Array 3 var frukt = ["eple", "banan", "pære"]; ["eple", "banan", "pære"]
  • 13. Array 4 • var frukt = ["eple", "banan", "pære"] • • • • frukt.pop(); // ["eple", "banan"] frukt.push("tomat"); // ["eple", "banan", "tomat"] frukt.shift(); // ["banan", "tomat"] frukt.unshift("tomat"); // ["tomat", "banan", "tomat"]
  • 15. Objects • • • • • Everything is an Object Booleans can be objects or primitive data treated as objects Numbers can be objects or primitive data treated as objects Strings are also objects or primitive data treated as objects Dates, Maths, Regular expressions, Arrays and functions are always objects
  • 16. Object literal An object is just a special kind of data, with properties and methods. var person = { firstName: "John", lastName: "Doe", id: 5 };
  • 17. Object literal An object is just a special kind of data, with properties and methods. var person = { firstName: "John", lastName: "Doe", id: 5 }; person.id; // 5
  • 18. Object literal An object is just a special kind of data, with properties and methods. var person = { firstName: "John", lastName: "Doe", address: { street: "Strandsvingen", number: "14B" } }; person.address.street; // "Strandsvingen"
  • 19. Functions a block of code that will be executed when "someone" calls it: function add(a, b) { return a + b; } var add = function(a, b) { return a + b; }
  • 20. Object Constructor function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } var randomPerson = new Person("John", "Doe");
  • 21. Object var randomPerson = new Object(); randomPerson.firstName = "John"; randomPerson.lastName = "Doe";
  • 22. Boolean expressions if (a == 2) {//if this is true //do this... }
  • 23. Type coercion • When JavaScript are unsure what you mean, It makes a guess • Example: if ('false') { console.log("true"); } • «A String is obviously not true or false, you probably mean true!»
  • 24. True!  • if (true) { alert("true"); } // alerts "true" • if ('false') { alert("true"); } // alerts "true" • if ([]) { alert("true"); } // alerts "true" • if (5) { alert("true"); } // alerts "true" • if ({}) { alert("true"); } // alerts "true"
  • 25. False  • if (false) { alert("false"); } // does nothing • if (0) { alert("false"); } // does nothing • if (null) { alert("false"); } // does nothing • if (undefined) { alert("false"); } // does nothing • if ("") { alert("false"); } // does nothing
  • 26. More type coercion  1 == "1" true == "1" false == "0"
  • 27. More type coercion  1 == "1" true true == "1" false == "0"
  • 28. More type coercion  1 == "1" true true == "1" true false == "0"
  • 29. More type coercion  1 == "1" true true == "1" true false == "0" true
  • 30. More type coercion 1 == "1" true true == "1" true false == "0" true false == "0" == 1 == true == [] == ""
  • 31. More type coercion  1 == "1" true true == "1" true false == "0" true false == "0" == 1 == true == [] == "" true
  • 34. === 1 == true true 1 === true false 1 == "1" true 1 === "1" false
  • 35. Scope & global variables • C#/Java: anything inside curly brackets, {} , defines a scope • Example: if (true) { var scopeVariable = "Test"; } scopeVariable = "Test2"; // variable not defined
  • 36. Scope & global variables • Javascript: only functions define a new scope • Example: if (true) { var scopeVariable = "Test"; } scopeVariable; // value: "Test";
  • 37. Scope & global variables function scope1() { var member; //is a member of the scope defined by the function example //this function is also part of the scope of the function example var innerScope = function() { member= 12; // traverses scope and assigns member in scope1 to 12 }; };
  • 38. Scope & global variables function scope1() { var member; //is a member of the scope defined by the function example //this function is also part of the scope of the function example var innerScope = function() { var member= 12; // defines member in this scope and do not traverse }; };
  • 39. Scope & global variables function scope1() { var member; //is a member of the scope defined by the function example //this function is also part of the scope of the function example var bar = function() { member= 12; // traverses scope and assigns scope1member.foo to 12 }; }; function scope2() { member = 15; // traverses scope and assigns global.member to 15 }
  • 40. Namespaces • Not built into JavaScript • Problem?
  • 41. JavaScript (Ad-hoc) namespace var Peanuts = {}; // Object used as namespace
  • 42. JavaScript (Ad-hoc) namespace var Peanuts = Peanuts || {}; // in case it already exists
  • 43. «Classes» and «methods» ? var Peanuts = Peanuts || {}; Peanuts.Calculator = { add: function (a,b) { return a + b; }, subtract: function () { return a - b; } }; Peanuts.Calculator.add(1, 2); // 3
  • 44. Immediately invoked function expressions • (function () { • // logic/code here • })();
  • 45. Why? • Executes an expression and therefore code immediately • Creates «privacy» for your code (function scope ftw!)
  • 46. How? • JavaScript, parenthesis can’t contain statements. • When the parser encounters the function keyword, it knows to parse it as a function expression and not a function declaration.
  • 47. Immediately invoked function expressions • • • • • (function () { // logic/code here })(); The key thing about JavaScript expressions is that they return values. To invoke the function expression right away we just need to tackle a couple of parentheses on the end(like above).
  • 48. Immediately invoked function expressions • (function (innerValue) { • // logic/code here • })(outerValue);
  • 49. Revealing module pattern var Peanuts = Peanuts || {}; Peanuts.Calculator = function () { var add = function(a, b) { return a + b; }; var subtract = function(a, b) { return a - b; }; return { add: add, subtract: subtract }; }; Peanuts.Calculator().add(1, 2); // 3
  • 50. Revealing module pattern 2 var Peanuts = Peanuts || {}; Peanuts.Calculator = (function () { var add = function(a, b) { return a + b; }; return { add: add, }; })(); Peanuts.Calculator.add(1, 2); // 3
  • 51. Revealing module pattern 3 var Peanuts = Peanuts || {}; Peanuts.Calculator = (function () { var PI = 3.14; // private variable! var getCircleArea = function(r) { return PI * r * r; }; return { getCircleArea: getCircleArea // public method }; })(); Peanuts.Calculator.getCircleArea(2); // 12.56