\u0110\u00e1nh gi\u00e1 post<\/span>\n <\/div>\n<\/div>\nD\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00e0i th\u1ee7 thu\u1eadt trong JavaScript m\u00e0 m\u00ecnh \u0111\u00e3 s\u01b0u t\u1ea7m v\u00e0 t\u00ecm hi\u1ec3u \u0111\u01b0\u1ee3c. Hi v\u1ecdng s\u1ebd gi\u00fap c\u00e1c b\u1ea1n t\u1ed1i \u01b0u code trong Javascript.<\/p>\n
To\u00e1n t\u1eed !!<\/h1>\n
S\u1eed d\u1ee5ng to\u00e1n t\u1eed !! s\u1ebd gi\u00fap ch\u00fang ta ki\u1ec3m tra \u0111\u01b0\u1ee3c m\u1ed9t v\u00e0i gi\u00e1 tr\u1ecb \u0111\u1eb7c bi\u1ec7t nh\u01b0 0<\/code>, null<\/code>, \"\"<\/code>, undefined<\/code>, NaN<\/code>. Khi \u0111\u1eb7t !!<\/code> tr\u01b0\u1edbc c\u00e1c gi\u00e1 tr\u1ecb tr\u00ean, th\u00ec s\u1ebd tr\u1ea3 v\u1ec1 false<\/code>, ng\u01b0\u1ee3c l\u1ea1i s\u1ebd tr\u1ea3 v\u1ec1 true<\/code>. V\u00ed d\u1ee5:<\/p>\n\n
<\/div>\n
\n
\n\n\n\n\n 1<\/div>\n 2<\/div>\n 3<\/div>\n 4<\/div>\n 5<\/div>\n 6<\/div>\n 7<\/div>\n 8<\/div>\n<\/div>\n<\/td>\n \n\n function<\/span> test<\/span>(<\/span>demo<\/span>)<\/span> {<\/span><\/div>\nconsole<\/span>.<\/span>log<\/span>(<\/span>!<\/span>!<\/span>demo<\/span>)<\/span>;<\/span><\/div>\n}<\/span><\/div>\n<\/div>\n test<\/span>(<\/span>1<\/span>)<\/span>;<\/span><\/div>\n\/\/ true<\/span><\/div>\ntest<\/span>(<\/span>0<\/span>)<\/span>;<\/span><\/div>\n\/\/ false<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\nTo\u00e1n t\u1eed +<\/h1>\nTo\u00e1n t\u1eed n\u00e0y gi\u00fap ta chuy\u1ec3n 1 chu\u1ed7i c\u00e1c s\u1ed1 (string) sang number. Ho\u1eb7c c\u00f3 th\u1ec3 ki\u1ec3m tra xem chu\u1ed7i \u0111\u00f3 c\u00f3 ph\u1ea3i l\u00e0 m\u1ed9t chu\u1ed7i s\u1ed1 hay kh\u00f4ng. B\u1edfi v\u00ec h\u00e0m s\u1ebd tr\u1ea3 v\u1ec1 NaN \u0111\u1ed1i v\u1edbi c\u00e1c chu\u1ed7i c\u00f3 ch\u1ee9a k\u00ed t\u1ef1 kh\u00f4ng ph\u1ea3i s\u1ed1.<\/p>\n \n <\/div>\n \n \n\n\n\n\n 1<\/div>\n 2<\/div>\n 3<\/div>\n 4<\/div>\n 5<\/div>\n 6<\/div>\n 7<\/div>\n 8<\/div>\n<\/div>\n<\/td>\n \n\n function<\/span> test<\/span>(<\/span>demo<\/span>)<\/span> {<\/span><\/div>\n\u00a0\u00a0<\/span>console<\/span>.<\/span>log<\/span>(<\/span>+<\/span>demo<\/span>)<\/span>;<\/span><\/div>\n}<\/span><\/div>\n<\/div>\n test<\/span>(<\/span>“123”<\/span>)<\/span>;<\/span><\/div>\n\/\/ 123<\/span><\/div>\ntest<\/span>(<\/span>“hello”<\/span>)<\/span>;<\/span><\/div>\n\/\/ NaN<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\nTo\u00e1n t\u1eed ||<\/h1>\n\u0110\u1ed1i v\u1edbi to\u00e1n t\u1eed ||<\/code>, m\u1ee5c \u0111\u00edch l\u00e0 \u0111\u1ec3 ch\u00e8n gi\u00e1 tr\u1ecb m\u1eb7c \u0111i\u1ecbnh nh\u01b0 l\u00e0 tham s\u1ed1 th\u1ee9 hai. Gi\u1ea3 s\u1eed tham s\u1ed1 \u0111\u1ea7u ti\u00ean tr\u1ea3 v\u1ec1 false<\/code> th\u00ec tham s\u1ed1 th\u1ee9 hai s\u1ebd \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng nh\u01b0 l\u00e0 m\u1ed9t gi\u00e1 tr\u1ecb m\u1eb7c \u0111\u1ecbnh.<\/p>\n\n <\/div>\n \n \n\n\n\n\n 1<\/div>\n 2<\/div>\n<\/div>\n<\/td>\n \n\n var<\/span> a<\/span> =<\/span> null<\/span> ||<\/span> 1<\/span>;<\/span><\/div>\n\/\/ a = 1<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\nS\u1eed d\u1ee5ng v\u00f2ng l\u1eb7p \u0111\u1ed1i v\u1edbi m\u1ea3ng<\/h1>\nCh\u00fang ta th\u01b0\u1eddng c\u00f3 th\u00f3i quen khi ch\u1ea1y m\u1ed9t v\u00f2ng l\u1eb7p \u0111\u1ed1i v\u1edbi m\u1ea3ng nh\u01b0 sau:<\/p>\n \n <\/div>\n \n \n\n\n\n\n 1<\/div>\n 2<\/div>\n 3<\/div>\n 4<\/div>\n<\/div>\n<\/td>\n \n\n var<\/span> array<\/span> =<\/span> [<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>,<\/span> 4<\/span>,<\/span> 5<\/span>,<\/span> 6<\/span>,<\/span> 7<\/span>,<\/span> 8<\/span>,<\/span> 9<\/span>,<\/span> 10<\/span>]<\/span>;<\/span><\/div>\nfor<\/span> (<\/span>var<\/span> i<\/span> =<\/span> 0<\/span>;<\/span> i<\/span> <<\/span> array<\/span>.<\/span>length<\/span>;<\/span> i<\/span>++<\/span>)<\/span> {<\/span><\/div>\n\u00a0\u00a0<\/span>console<\/span>.<\/span>log<\/span>(<\/span>array<\/span>[<\/span>i<\/span>]<\/span>)<\/span>;<\/span><\/div>\n}<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\nN\u1ebfu nh\u01b0 l\u00e0m nh\u01b0 v\u1eady, c\u1ee9 \u1ee9ng v\u1edbi m\u1ed9t v\u00f2ng l\u1eb7p s\u1ebd ph\u1ea3i t\u00ednh l\u1ea1i \u0111\u1ed9 r\u1ed9ng c\u1ee7a m\u1ea3ng. Nh\u01b0 v\u1eady s\u1ebd \u1ea3nh h\u01b0\u1edfng t\u1edbi hi\u1ec7u su\u1ea5t c\u1ee7a h\u1ec7 th\u1ed1ng. Thay v\u00ec th\u1ebf, ch\u00fang ta c\u00f3 th\u1ec3 kh\u1edfi t\u1ea1o m\u1ed9t bi\u1ebfn g\u00e1n gi\u00e1 tr\u1ecb l\u00e0 \u0111\u1ed9 r\u1ed9ng c\u1ee7a m\u1ea3ng, t\u1eeb \u0111\u00f3 s\u1ebd d\u1ec5 d\u00e0ng h\u01a1n trong v\u00f2ng l\u1eb7p.<\/p>\n \n <\/div>\n \n \n\n\n\n\n 1<\/div>\n 2<\/div>\n 3<\/div>\n 4<\/div>\n 5<\/div>\n<\/div>\n<\/td>\n \n \n | | | | | | | | | |