function bar(foo) {
if (foo) {
var test = "hello world";
}
// "hello world"
console.log(test);
}
function bar(foo) {
if (foo) {
let test = "hello world";
}
// throws exception
console.log(test);
}
function bar(foo) {
const test = "hello world";
if (foo) {
// Uncaught TypeError: Assignment to constant variable.
test = "hi world";
}
}
numbers.map(function(number) {
return number * 2;
});
numbers.map(number => number * 2);
numbers.map(function(number) {
return number * 2;
}.bind(this));
numbers.map(number => number * 2);
function getHTML(className) {
return '<div class="' + className + '">Hello World</div>';
}
function getHTML(className) {
return `<div class="${className}">Hello World</div>`;
}
Math.max(1, 8, 2, 5); // 8
const numbers = [1, 8, 2, 5];
Math.max(numbers[0], numbers[1], numbers[2], numbers[3]); // 8
const numbers = [1, 8, 2, 5];
Math.max.apply(Math, numbers); // 8
const numbers = [1, 8, 2, 5];
Math.max(...numbers); // 8
var myObject = {
a: 'foo',
b: 'bar',
c: 'hello'
};
var a = myObject.a;
var b = myObject.b;
var c = myObject.c;
const { a, b, c} = myObject;
React is a declarative, efficient, and flexible JavaScript library for building user interfaces
<input placeholder="Enter your name" />
<inputCount placeholder="Enter your name" />
class InputCount extends React.Component {
render() {
return (
<input placeholder="Enter your name" />
);
}
}
<InputCount />
class InputCount extends React.Component {
constructor() {
}
render() {
return (
<input placeholder="Enter your name" />
);
}
}
class InputCount extends React.Component {
constructor() {
this.state = { count: 0 };
}
render() {
return (
<input placeholder="Enter your name" />
);
}
}
class InputCount extends React.Component {
constructor() {
this.state = { count: 0 };
this.handleKey = this.handleKey.bind(this);
}
handleKey(event) {
}
render() {
return (
<input
placeholder="Enter your name"
onkeyup={this.handleKey}
/>
);
}
}
class InputCount extends React.Component {
constructor() {
this.state = { count: 0 };
this.handleKey = this.handleKey.bind(this);
}
handleKey(event) {
this.setState({ count: event.target.value.length });
}
render() {
return (
<input
placeholder="Enter your name"
onkeyup={this.handleKey}
/>
);
}
}
render() {
return (
<div>
<input
placeholder="Enter your name"
onkeyup={this.handleKey}
/>
<span>{this.state.count}</span>
</div>
);
}
<InputCount />
render() {
return (
<div>
<input
placeholder="Enter your name"
onkeyup={this.handleKey}
/>
<span>{this.state.count}</span>
</div>
);
}
render() {
return (
<div>
<input
placeholder={this.props.defaultText}
onkeyup={this.handleKey}
/>
<span>{this.state.count}</span>
</div>
);
}
<InputCount defaultText="Enter your phone number" />
<InputCount defaultText="Enter your name" />
<InputCount defaultText="Enter your phone number" />
<InputCount defaultText="Enter your address" />
Redux is a predictable state container for JavaScript apps
function selectColor(color) {
return {
type: "SELECT_COLOR",
color: color
};
}
this.props.dispatch(selectColor(color));
function colorReducer(state, action) {
switch (action.type) {
case "SELECT_COLOR":
return Object.assign({}, state, {
color: action.color
});
default:
return state;
}
}
npm install -g create-react-app
create-react-app my-app
cd my-app/
npm start
npm run build