소스 코드를 기록하는 남자

JavaScript - bool data type converting

JavaScript

 

False

Boolean(0)
Boolean(Nan)
Boolean('')
Boolean(null)
Boolean(undefined)

 

these 5 cases are returning false. Except these cases are returning true

 

Equality Operator  
== both side value are same
!= both side value are different
=== both side value and data type are same
!== both side value and data type are different

 

'JavaScript' 카테고리의 다른 글

JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14

JavaScript - typeof(), prompt(), String(), Number()

JavaScript

typeof() : to check the data type like number, string, bool 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>title</title>
<script type="text/javascript">
var str = "This is String";
var num = 500;
var bool = true;
alert(typeof(str));
alert(typeof(num));
alert(typeof(bool));
</script>
</head>
<body>

</body>
</html>

alert(typeof(str));

 

alert(typeof(num));
alert(typeof(bool));

prompt(String message, String default) : a function to get a value for a variable

var input = prompt("Message", "Hello")

 

 alert(input) : Input is "Hello" 

Number() : coverting data type as number. if its not a number, converting to "Nan"

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>title</title>
<script type="text/javascript">

var input = prompt("number input", "number");
var numberInput = Number(input);

alert(typeof(numberInput)+": "+numberInput);
</script>
</head>
<body>

</body>
</html>

String() : same as Number() function

'JavaScript' 카테고리의 다른 글

JavaScript - bool data type converting  (0) 2019.09.14
JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14

JavaScript - Function Executing Process

JavaScript

Because of JavaScript's Declaration Process, Every Declarations will go to the Top of Code

without value

 

Without Value is very important. Let's look the example

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>함수의 동작원리 :: 선언적 함수 : 리턴타입+매개변수</title>
<script type="text/javascript">

	var calc = function (x, y) {//function declaration
		return x+y;
	}	
	console.log(calc(5,3)); //8
	
	

	var calc = function (x, y) {//function declaration
		return x*y;
	}
	console.log(calc(5,3)); //15
	
</script>
</head>
<body>

</body>
</html>

first console.log will print 8, second will print 15.

 

this is the process of this code.

 

1. var calc will go to the top

 

2. x + y function will be a value of calc.

 

3. first console.log print 5+3

 

4. x * y function will be a value of calc.

 

5. second console.log print 5 * 3

 

 

But it is different between function and variable, lets see the example

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>선언적 함수 정의 및 재정의 | 익명함수 정의 및 재정의</title>
<script type="text/javascript">

 	
	var func = function(){alert("func inner");}// 2 second create
	function func(){alert("func declear");} //1 first create
	func(); //func inner; 
	
	
</script>
</head>
<body>

</body>
</html>

for this code, variable func and function func() will go to the top.

 

so this process is

 

1. func will be go to the top with first create function

 

2. func is overrided with second create.

 

3. func() executing

'JavaScript' 카테고리의 다른 글

JavaScript - bool data type converting  (0) 2019.09.14
JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14

JavaScript - Global Variable

JavaScript

In javaScript, it is different from other langauge.

 

Lets see the code.

 

after showAge function called, the variable "age" is changed to a Global Variable 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">

	// Global Variable Declaration (3) a Global variable connected to window object
/* 	var myName = "James";
	//
	firstName = "Gosling";
	//
	var name;
	name = "James Gosling";
	
	console.log(myName +' '+firstName+" "+name); */
	
	//2. A Global Variable with function
	
	function showAge() {
		age = 90;
		//console.log(age);
	}
	showAge(); //after function called, age will be a global variable
	console.log(age);
	
	
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - typeof(), prompt(), String(), Number()  (0) 2019.09.14
JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14

JavaScript - Hoisting

JavaScript

In JavaScript, there is a rule for Declaration.

 

Every variable will be hoisted to top. 

 

Remember! Just Declaration, Not a value

 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>호이스팅 :: Hoisting</title>
<script type="text/javascript">
//1. hoisting
 	doSomething();
	
	function doSomething() { 
		alert("not defined : "+some); //undefined
	
		var some = "WellBeing";
		
		alert("defined : "+some); //WellBeing
	}; 
//2. Global Variable
var name = "James";
function showName(){
	var name = "Gosling";
	console.log(name);
}
console.log(name);
showName();
</script>
</head>
<body>

</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - Function Executing Process  (0) 2019.09.14
JavaScript - Global Variable  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14
JavaScript - Datatype  (0) 2019.09.14

JavaScript - window.onload

JavaScript

What is window.onload?

1. A function excutes after All contents are loaded.

 

2. the only one window.onload exists in a document

 

3. it is possible using onload to body tag. for this case, window.onload is ignored. 
   priority ( body tag onload > window.onload )

 

<!-- 
window.onload = function(){}
1. html 문서에 포함된 모든 콘텐츠가 로드된 후에 실행하는 함수
2. 동일한 문서에 window.onload 는 단 하나만 존재해야한다.
3. body 태그에도 onload 속성을 지정할 수 있는데 이때 window.onload 속성은 무시된다.
 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">

	window.onload = function(){	alert("First window.onload");	};
	window.onload = function(){	alert("Second window.onload");	};
	
	/* If using onloads method, last onload will excute (Overriding) */
	
	/* jquery*/
	$(document).ready(function() {
		alert("First ready..");
	});

	$(document).ready(function() {
		alert("Second ready..");
	});
	
	
</script>
</head>
<!-- <body onload = "loading()"> -->
<body>
<h2>
hi~
</h2>
</body>
</html>

'JavaScript' 카테고리의 다른 글

JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14
JavaScript - Datatype  (0) 2019.09.14
What is JavaScript?  (0) 2019.09.14

JavaScript - Escape character, Operator

JavaScript

Escape Character

Escape Character Description
\t Horizontal tab
\n Line break
\' Single quote
\" Double quote
\\ Backslash

Operator

Operator Desc Operator Desc
- minus / divide
+ plus * multiple

example

<!-- 
window.onload = function(){}
1. html 문서에 포함된 모든 콘텐츠가 로드된 후에 실해오디는 함수
2. 동일한 문서에 window.onload 는 단 하나만 존재해야한다.
3. body 태그에도 onload 속성을 지정할 수 있는데 이때 window.onload 속성은 무시된다.
 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
<script type="text/javascript">

	alert(5+3*2);
    alert((5-3)/2);
</script>
</head>
<body>
</body>
</html>​

'JavaScript' 카테고리의 다른 글

JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Datatype  (0) 2019.09.14
What is JavaScript?  (0) 2019.09.14

JavaScript - Datatype

JavaScript

https://www.splessons.com/lesson/javascript-datatypes/

 

Notion : <script type="text/javascript"></script> -> this block is the JavaScript

 

There are 6 Basic Datatypes in JavaScript.

 

1. String : Text

 

2. number : Integer, real number

 

3. boolean : true, false

 

4. function : method

 

5. object : a Data and a Concept includes every function, process related to Data.

 

More Detail for Objects is here
https://www.geeksforgeeks.org/objects-in-javascript/

 

Objects in Javascript - GeeksforGeeks

Objects, in JavaScript, is it’s most important data-type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScript’s primitive data-types(Number,… Read More »

www.geeksforgeeks.org

 

6. undefined : Nothing more Detail go this bookmark

 

https://codeburst.io/javascript-whats-the-difference-between-null-undefined-37793b5bfce6

 

 

alert() : A method for JavaScript 

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>

<script type="text/javascript">
	var variable;
	var stringVar = "String";
	var numberVar = 123;
	var bolVar = true;
	var functionVar = function(){}; // anonymous function
	var objectVar ={};
	
	alert(typeof stringVar);
	alert(typeof numberVar);
	alert(typeof bolVar);
	alert(typeof functionVar);
	alert(typeof objectVar);
	alert(typeof alpha); //undefined
	alert(typeof variable); //undefined
	
	
</script>

</head>
<body>

</body>
</html>

 

'JavaScript' 카테고리의 다른 글

JavaScript - Global Variable  (0) 2019.09.14
JavaScript - Hoisting  (0) 2019.09.14
JavaScript - window.onload  (0) 2019.09.14
JavaScript - Escape character, Operator  (0) 2019.09.14
What is JavaScript?  (0) 2019.09.14